@@ -1226,8 +1226,9 @@ static int context_read_and_validate(struct context *c, struct policydb *p,
1226
1226
* binary representation file.
1227
1227
*/
1228
1228
1229
- int str_read (char * * strp , gfp_t flags , struct policy_file * fp , u32 len )
1229
+ int str_read (char * * strp , gfp_t flags , struct policy_file * fp , u32 len , int kind , u32 max_len )
1230
1230
{
1231
+ u32 i ;
1231
1232
int rc ;
1232
1233
char * str ;
1233
1234
@@ -1237,19 +1238,35 @@ int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len)
1237
1238
if (size_check (sizeof (char ), len , fp ))
1238
1239
return - EINVAL ;
1239
1240
1241
+ if (max_len != 0 && len > max_len )
1242
+ return - EINVAL ;
1243
+
1240
1244
str = kmalloc (len + 1 , flags | __GFP_NOWARN );
1241
1245
if (!str )
1242
1246
return - ENOMEM ;
1243
1247
1244
1248
rc = next_entry (str , fp , len );
1245
- if (rc ) {
1246
- kfree (str );
1247
- return rc ;
1249
+ if (rc )
1250
+ goto bad_str ;
1251
+
1252
+ rc = - EINVAL ;
1253
+ for (i = 0 ; i < len ; i ++ ) {
1254
+ if (iscntrl (str [i ]))
1255
+ goto bad_str ;
1256
+
1257
+ if (kind == STR_IDENTIFIER &&
1258
+ !(isalnum (str [i ]) || str [i ] == '_' || str [i ] == '-' || str [i ] == '.' ))
1259
+ goto bad_str ;
1260
+
1248
1261
}
1249
1262
1250
1263
str [len ] = '\0' ;
1251
1264
* strp = str ;
1252
1265
return 0 ;
1266
+
1267
+ bad_str :
1268
+ kfree (str );
1269
+ return rc ;
1253
1270
}
1254
1271
1255
1272
static int perm_read (struct policydb * p , struct symtab * s , struct policy_file * fp )
@@ -1274,7 +1291,7 @@ static int perm_read(struct policydb *p, struct symtab *s, struct policy_file *f
1274
1291
if (perdatum -> value < 1 || perdatum -> value > SEL_VEC_MAX )
1275
1292
goto bad ;
1276
1293
1277
- rc = str_read (& key , GFP_KERNEL , fp , len );
1294
+ rc = str_read_perm (& key , GFP_KERNEL , fp , len );
1278
1295
if (rc )
1279
1296
goto bad ;
1280
1297
@@ -1321,7 +1338,7 @@ static int common_read(struct policydb *p, struct symtab *s, struct policy_file
1321
1338
goto bad ;
1322
1339
comdatum -> permissions .nprim = le32_to_cpu (buf [2 ]);
1323
1340
1324
- rc = str_read (& key , GFP_KERNEL , fp , len );
1341
+ rc = str_read_class (& key , GFP_KERNEL , fp , len );
1325
1342
if (rc )
1326
1343
goto bad ;
1327
1344
@@ -1559,12 +1576,12 @@ static int class_read(struct policydb *p, struct symtab *s, struct policy_file *
1559
1576
1560
1577
ncons = le32_to_cpu (buf [5 ]);
1561
1578
1562
- rc = str_read (& key , GFP_KERNEL , fp , len );
1579
+ rc = str_read_class (& key , GFP_KERNEL , fp , len );
1563
1580
if (rc )
1564
1581
goto bad ;
1565
1582
1566
1583
if (len2 ) {
1567
- rc = str_read (& cladatum -> comkey , GFP_KERNEL , fp , len2 );
1584
+ rc = str_read_class (& cladatum -> comkey , GFP_KERNEL , fp , len2 );
1568
1585
if (rc )
1569
1586
goto bad ;
1570
1587
@@ -1698,7 +1715,7 @@ static int role_read(struct policydb *p, struct symtab *s, struct policy_file *f
1698
1715
if (p -> policyvers >= POLICYDB_VERSION_BOUNDARY )
1699
1716
role -> bounds = le32_to_cpu (buf [2 ]);
1700
1717
1701
- rc = str_read (& key , GFP_KERNEL , fp , len );
1718
+ rc = str_read_role (& key , GFP_KERNEL , fp , len );
1702
1719
if (rc )
1703
1720
goto bad ;
1704
1721
@@ -1765,7 +1782,7 @@ static int type_read(struct policydb *p, struct symtab *s, struct policy_file *f
1765
1782
typdatum -> primary = le32_to_cpu (buf [2 ]);
1766
1783
}
1767
1784
1768
- rc = str_read (& key , GFP_KERNEL , fp , len );
1785
+ rc = str_read_type (& key , GFP_KERNEL , fp , len );
1769
1786
if (rc )
1770
1787
goto bad ;
1771
1788
@@ -1829,7 +1846,7 @@ static int user_read(struct policydb *p, struct symtab *s, struct policy_file *f
1829
1846
if (p -> policyvers >= POLICYDB_VERSION_BOUNDARY )
1830
1847
usrdatum -> bounds = le32_to_cpu (buf [2 ]);
1831
1848
1832
- rc = str_read (& key , GFP_KERNEL , fp , len );
1849
+ rc = str_read_user (& key , GFP_KERNEL , fp , len );
1833
1850
if (rc )
1834
1851
goto bad ;
1835
1852
@@ -1878,7 +1895,7 @@ static int sens_read(struct policydb *p, struct symtab *s, struct policy_file *f
1878
1895
goto bad ;
1879
1896
levdatum -> isalias = val ;
1880
1897
1881
- rc = str_read (& key , GFP_KERNEL , fp , len );
1898
+ rc = str_read_sens (& key , GFP_KERNEL , fp , len );
1882
1899
if (rc )
1883
1900
goto bad ;
1884
1901
@@ -1921,7 +1938,7 @@ static int cat_read(struct policydb *p, struct symtab *s, struct policy_file *fp
1921
1938
goto bad ;
1922
1939
catdatum -> isalias = val ;
1923
1940
1924
- rc = str_read (& key , GFP_KERNEL , fp , len );
1941
+ rc = str_read_cat (& key , GFP_KERNEL , fp , len );
1925
1942
if (rc )
1926
1943
goto bad ;
1927
1944
@@ -2230,7 +2247,7 @@ static int filename_trans_read_helper_compat(struct policydb *p, struct policy_f
2230
2247
len = le32_to_cpu (buf [0 ]);
2231
2248
2232
2249
/* path component string */
2233
- rc = str_read (& name , GFP_KERNEL , fp , len );
2250
+ rc = str_read (& name , GFP_KERNEL , fp , len , STR_UNCONSTRAINT , 0 );
2234
2251
if (rc )
2235
2252
return rc ;
2236
2253
@@ -2329,7 +2346,7 @@ static int filename_trans_read_helper(struct policydb *p, struct policy_file *fp
2329
2346
len = le32_to_cpu (buf [0 ]);
2330
2347
2331
2348
/* path component string */
2332
- rc = str_read (& name , GFP_KERNEL , fp , len );
2349
+ rc = str_read (& name , GFP_KERNEL , fp , len , STR_UNCONSTRAINT , 0 );
2333
2350
if (rc )
2334
2351
return rc ;
2335
2352
@@ -2483,7 +2500,7 @@ static int genfs_read(struct policydb *p, struct policy_file *fp)
2483
2500
if (!newgenfs )
2484
2501
goto out ;
2485
2502
2486
- rc = str_read (& newgenfs -> fstype , GFP_KERNEL , fp , len );
2503
+ rc = str_read (& newgenfs -> fstype , GFP_KERNEL , fp , len , STR_IDENTIFIER , 128 );
2487
2504
if (rc )
2488
2505
goto out ;
2489
2506
@@ -2522,7 +2539,7 @@ static int genfs_read(struct policydb *p, struct policy_file *fp)
2522
2539
if (!newc )
2523
2540
goto out ;
2524
2541
2525
- rc = str_read (& newc -> u .name , GFP_KERNEL , fp , len );
2542
+ rc = str_read (& newc -> u .name , GFP_KERNEL , fp , len , STR_UNCONSTRAINT , 0 );
2526
2543
if (rc )
2527
2544
goto out ;
2528
2545
@@ -2625,7 +2642,7 @@ static int ocontext_read(struct policydb *p,
2625
2642
goto out ;
2626
2643
len = le32_to_cpu (buf [0 ]);
2627
2644
2628
- rc = str_read (& c -> u .name , GFP_KERNEL , fp , len );
2645
+ rc = str_read (& c -> u .name , GFP_KERNEL , fp , len , STR_IDENTIFIER , 128 );
2629
2646
if (rc )
2630
2647
goto out ;
2631
2648
@@ -2693,7 +2710,7 @@ static int ocontext_read(struct policydb *p,
2693
2710
goto out ;
2694
2711
2695
2712
len = le32_to_cpu (buf [1 ]);
2696
- rc = str_read (& c -> u .name , GFP_KERNEL , fp , len );
2713
+ rc = str_read (& c -> u .name , GFP_KERNEL , fp , len , STR_IDENTIFIER , 128 );
2697
2714
if (rc )
2698
2715
goto out ;
2699
2716
@@ -2759,7 +2776,7 @@ static int ocontext_read(struct policydb *p,
2759
2776
len = le32_to_cpu (buf [0 ]);
2760
2777
2761
2778
rc = str_read (& c -> u .ibendport .dev_name ,
2762
- GFP_KERNEL , fp , len );
2779
+ GFP_KERNEL , fp , len , STR_IDENTIFIER , 128 );
2763
2780
if (rc )
2764
2781
goto out ;
2765
2782
@@ -2827,7 +2844,8 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
2827
2844
goto bad ;
2828
2845
}
2829
2846
2830
- rc = str_read (& policydb_str , GFP_KERNEL , fp , len );
2847
+ rc = str_read (& policydb_str , GFP_KERNEL , fp , len ,
2848
+ STR_UNCONSTRAINT , strlen (POLICYDB_STRING ));
2831
2849
if (rc ) {
2832
2850
if (rc == - ENOMEM ) {
2833
2851
pr_err ("SELinux: unable to allocate memory for policydb string of length %d\n" ,
0 commit comments