@@ -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 (oom_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 > 32 )
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
@@ -1320,7 +1337,7 @@ static int common_read(struct policydb *p, struct symtab *s, struct policy_file
1320
1337
goto bad ;
1321
1338
comdatum -> permissions .nprim = le32_to_cpu (buf [2 ]);
1322
1339
1323
- rc = str_read (& key , GFP_KERNEL , fp , len );
1340
+ rc = str_read_class (& key , GFP_KERNEL , fp , len );
1324
1341
if (rc )
1325
1342
goto bad ;
1326
1343
@@ -1557,12 +1574,12 @@ static int class_read(struct policydb *p, struct symtab *s, struct policy_file *
1557
1574
1558
1575
ncons = le32_to_cpu (buf [5 ]);
1559
1576
1560
- rc = str_read (& key , GFP_KERNEL , fp , len );
1577
+ rc = str_read_class (& key , GFP_KERNEL , fp , len );
1561
1578
if (rc )
1562
1579
goto bad ;
1563
1580
1564
1581
if (len2 ) {
1565
- rc = str_read (& cladatum -> comkey , GFP_KERNEL , fp , len2 );
1582
+ rc = str_read_class (& cladatum -> comkey , GFP_KERNEL , fp , len2 );
1566
1583
if (rc )
1567
1584
goto bad ;
1568
1585
@@ -1696,7 +1713,7 @@ static int role_read(struct policydb *p, struct symtab *s, struct policy_file *f
1696
1713
if (p -> policyvers >= POLICYDB_VERSION_BOUNDARY )
1697
1714
role -> bounds = le32_to_cpu (buf [2 ]);
1698
1715
1699
- rc = str_read (& key , GFP_KERNEL , fp , len );
1716
+ rc = str_read_role (& key , GFP_KERNEL , fp , len );
1700
1717
if (rc )
1701
1718
goto bad ;
1702
1719
@@ -1763,7 +1780,7 @@ static int type_read(struct policydb *p, struct symtab *s, struct policy_file *f
1763
1780
typdatum -> primary = le32_to_cpu (buf [2 ]);
1764
1781
}
1765
1782
1766
- rc = str_read (& key , GFP_KERNEL , fp , len );
1783
+ rc = str_read_type (& key , GFP_KERNEL , fp , len );
1767
1784
if (rc )
1768
1785
goto bad ;
1769
1786
@@ -1827,7 +1844,7 @@ static int user_read(struct policydb *p, struct symtab *s, struct policy_file *f
1827
1844
if (p -> policyvers >= POLICYDB_VERSION_BOUNDARY )
1828
1845
usrdatum -> bounds = le32_to_cpu (buf [2 ]);
1829
1846
1830
- rc = str_read (& key , GFP_KERNEL , fp , len );
1847
+ rc = str_read_user (& key , GFP_KERNEL , fp , len );
1831
1848
if (rc )
1832
1849
goto bad ;
1833
1850
@@ -1876,7 +1893,7 @@ static int sens_read(struct policydb *p, struct symtab *s, struct policy_file *f
1876
1893
goto bad ;
1877
1894
levdatum -> isalias = val ;
1878
1895
1879
- rc = str_read (& key , GFP_KERNEL , fp , len );
1896
+ rc = str_read_sens (& key , GFP_KERNEL , fp , len );
1880
1897
if (rc )
1881
1898
goto bad ;
1882
1899
@@ -1919,7 +1936,7 @@ static int cat_read(struct policydb *p, struct symtab *s, struct policy_file *fp
1919
1936
goto bad ;
1920
1937
catdatum -> isalias = val ;
1921
1938
1922
- rc = str_read (& key , GFP_KERNEL , fp , len );
1939
+ rc = str_read_cat (& key , GFP_KERNEL , fp , len );
1923
1940
if (rc )
1924
1941
goto bad ;
1925
1942
@@ -2225,7 +2242,7 @@ static int filename_trans_read_helper_compat(struct policydb *p, struct policy_f
2225
2242
len = le32_to_cpu (buf [0 ]);
2226
2243
2227
2244
/* path component string */
2228
- rc = str_read (& name , GFP_KERNEL , fp , len );
2245
+ rc = str_read (& name , GFP_KERNEL , fp , len , STR_UNCONSTRAINT , 0 );
2229
2246
if (rc )
2230
2247
return rc ;
2231
2248
@@ -2324,7 +2341,7 @@ static int filename_trans_read_helper(struct policydb *p, struct policy_file *fp
2324
2341
len = le32_to_cpu (buf [0 ]);
2325
2342
2326
2343
/* path component string */
2327
- rc = str_read (& name , GFP_KERNEL , fp , len );
2344
+ rc = str_read (& name , GFP_KERNEL , fp , len , STR_UNCONSTRAINT , 0 );
2328
2345
if (rc )
2329
2346
return rc ;
2330
2347
@@ -2478,7 +2495,7 @@ static int genfs_read(struct policydb *p, struct policy_file *fp)
2478
2495
if (!newgenfs )
2479
2496
goto out ;
2480
2497
2481
- rc = str_read (& newgenfs -> fstype , GFP_KERNEL , fp , len );
2498
+ rc = str_read (& newgenfs -> fstype , GFP_KERNEL , fp , len , STR_IDENTIFIER , 128 );
2482
2499
if (rc )
2483
2500
goto out ;
2484
2501
@@ -2517,7 +2534,7 @@ static int genfs_read(struct policydb *p, struct policy_file *fp)
2517
2534
if (!newc )
2518
2535
goto out ;
2519
2536
2520
- rc = str_read (& newc -> u .name , GFP_KERNEL , fp , len );
2537
+ rc = str_read (& newc -> u .name , GFP_KERNEL , fp , len , STR_UNCONSTRAINT , 0 );
2521
2538
if (rc )
2522
2539
goto out ;
2523
2540
@@ -2620,7 +2637,7 @@ static int ocontext_read(struct policydb *p,
2620
2637
goto out ;
2621
2638
len = le32_to_cpu (buf [0 ]);
2622
2639
2623
- rc = str_read (& c -> u .name , GFP_KERNEL , fp , len );
2640
+ rc = str_read (& c -> u .name , GFP_KERNEL , fp , len , STR_IDENTIFIER , 128 );
2624
2641
if (rc )
2625
2642
goto out ;
2626
2643
@@ -2688,7 +2705,7 @@ static int ocontext_read(struct policydb *p,
2688
2705
goto out ;
2689
2706
2690
2707
len = le32_to_cpu (buf [1 ]);
2691
- rc = str_read (& c -> u .name , GFP_KERNEL , fp , len );
2708
+ rc = str_read (& c -> u .name , GFP_KERNEL , fp , len , STR_IDENTIFIER , 128 );
2692
2709
if (rc )
2693
2710
goto out ;
2694
2711
@@ -2754,7 +2771,7 @@ static int ocontext_read(struct policydb *p,
2754
2771
len = le32_to_cpu (buf [0 ]);
2755
2772
2756
2773
rc = str_read (& c -> u .ibendport .dev_name ,
2757
- GFP_KERNEL , fp , len );
2774
+ GFP_KERNEL , fp , len , STR_IDENTIFIER , 128 );
2758
2775
if (rc )
2759
2776
goto out ;
2760
2777
@@ -2822,7 +2839,8 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
2822
2839
goto bad ;
2823
2840
}
2824
2841
2825
- rc = str_read (& policydb_str , GFP_KERNEL , fp , len );
2842
+ rc = str_read (& policydb_str , GFP_KERNEL , fp , len ,
2843
+ STR_UNCONSTRAINT , strlen (POLICYDB_STRING ));
2826
2844
if (rc ) {
2827
2845
if (rc == - ENOMEM ) {
2828
2846
pr_err ("SELinux: unable to allocate memory for policydb string of length %d\n" ,
0 commit comments