@@ -552,7 +552,8 @@ static int common_index(void *key, void *datum, void *datap)
552
552
553
553
comdatum = datum ;
554
554
p = datap ;
555
- if (!comdatum -> value || comdatum -> value > p -> p_commons .nprim )
555
+ if (!comdatum -> value || comdatum -> value > p -> p_commons .nprim ||
556
+ comdatum -> value > IDENTIFIER_MAXVALUE )
556
557
return - EINVAL ;
557
558
558
559
p -> sym_val_to_name [SYM_COMMONS ][comdatum -> value - 1 ] = key ;
@@ -567,7 +568,8 @@ static int class_index(void *key, void *datum, void *datap)
567
568
568
569
cladatum = datum ;
569
570
p = datap ;
570
- if (!cladatum -> value || cladatum -> value > p -> p_classes .nprim )
571
+ if (!cladatum -> value || cladatum -> value > p -> p_classes .nprim ||
572
+ cladatum -> value > IDENTIFIER_MAXVALUE )
571
573
return - EINVAL ;
572
574
573
575
p -> sym_val_to_name [SYM_CLASSES ][cladatum -> value - 1 ] = key ;
@@ -583,6 +585,7 @@ static int role_index(void *key, void *datum, void *datap)
583
585
role = datum ;
584
586
p = datap ;
585
587
if (!role -> value || role -> value > p -> p_roles .nprim ||
588
+ role -> value > IDENTIFIER_MAXVALUE ||
586
589
role -> bounds > p -> p_roles .nprim )
587
590
return - EINVAL ;
588
591
@@ -601,6 +604,7 @@ static int type_index(void *key, void *datum, void *datap)
601
604
602
605
if (typdatum -> primary ) {
603
606
if (!typdatum -> value || typdatum -> value > p -> p_types .nprim ||
607
+ typdatum -> value > IDENTIFIER_MAXVALUE ||
604
608
typdatum -> bounds > p -> p_types .nprim )
605
609
return - EINVAL ;
606
610
p -> sym_val_to_name [SYM_TYPES ][typdatum -> value - 1 ] = key ;
@@ -618,6 +622,7 @@ static int user_index(void *key, void *datum, void *datap)
618
622
usrdatum = datum ;
619
623
p = datap ;
620
624
if (!usrdatum -> value || usrdatum -> value > p -> p_users .nprim ||
625
+ usrdatum -> value > IDENTIFIER_MAXVALUE ||
621
626
usrdatum -> bounds > p -> p_users .nprim )
622
627
return - EINVAL ;
623
628
@@ -634,7 +639,8 @@ static int sens_index(void *key, void *datum, void *datap)
634
639
levdatum = datum ;
635
640
p = datap ;
636
641
637
- if (!levdatum -> level .sens || levdatum -> level .sens > p -> p_levels .nprim )
642
+ if (!levdatum -> level .sens || levdatum -> level .sens > p -> p_levels .nprim ||
643
+ levdatum -> level .sens > IDENTIFIER_MAXVALUE )
638
644
return - EINVAL ;
639
645
640
646
if (!levdatum -> isalias )
@@ -651,7 +657,8 @@ static int cat_index(void *key, void *datum, void *datap)
651
657
catdatum = datum ;
652
658
p = datap ;
653
659
654
- if (!catdatum -> value || catdatum -> value > p -> p_cats .nprim )
660
+ if (!catdatum -> value || catdatum -> value > p -> p_cats .nprim ||
661
+ catdatum -> value > IDENTIFIER_MAXVALUE )
655
662
return - EINVAL ;
656
663
657
664
if (!catdatum -> isalias )
@@ -1226,8 +1233,9 @@ static int context_read_and_validate(struct context *c, struct policydb *p,
1226
1233
* binary representation file.
1227
1234
*/
1228
1235
1229
- int str_read (char * * strp , gfp_t flags , struct policy_file * fp , u32 len )
1236
+ int str_read (char * * strp , gfp_t flags , struct policy_file * fp , u32 len , int kind , u32 max_len )
1230
1237
{
1238
+ u32 i ;
1231
1239
int rc ;
1232
1240
char * str ;
1233
1241
@@ -1237,19 +1245,35 @@ int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len)
1237
1245
if (size_check (sizeof (char ), len , fp ))
1238
1246
return - EINVAL ;
1239
1247
1248
+ if (len > max_len )
1249
+ return - EINVAL ;
1250
+
1240
1251
str = kmalloc (len + 1 , flags | __GFP_NOWARN );
1241
1252
if (!str )
1242
1253
return - ENOMEM ;
1243
1254
1244
1255
rc = next_entry (str , fp , len );
1245
- if (rc ) {
1246
- kfree (str );
1247
- return rc ;
1256
+ if (rc )
1257
+ goto bad_str ;
1258
+
1259
+ rc = - EINVAL ;
1260
+ for (i = 0 ; i < len ; i ++ ) {
1261
+ if (iscntrl (str [i ]))
1262
+ goto bad_str ;
1263
+
1264
+ if (kind == STR_IDENTIFIER &&
1265
+ !(isalnum (str [i ]) || str [i ] == '_' || str [i ] == '-' || str [i ] == '.' ))
1266
+ goto bad_str ;
1267
+
1248
1268
}
1249
1269
1250
1270
str [len ] = '\0' ;
1251
1271
* strp = str ;
1252
1272
return 0 ;
1273
+
1274
+ bad_str :
1275
+ kfree (str );
1276
+ return rc ;
1253
1277
}
1254
1278
1255
1279
static int perm_read (struct policydb * p , struct symtab * s , struct policy_file * fp )
@@ -1274,7 +1298,7 @@ static int perm_read(struct policydb *p, struct symtab *s, struct policy_file *f
1274
1298
if (perdatum -> value < 1 || perdatum -> value > SEL_VEC_MAX )
1275
1299
goto bad ;
1276
1300
1277
- rc = str_read (& key , GFP_KERNEL , fp , len );
1301
+ rc = str_read_perm (& key , GFP_KERNEL , fp , len );
1278
1302
if (rc )
1279
1303
goto bad ;
1280
1304
@@ -1321,7 +1345,7 @@ static int common_read(struct policydb *p, struct symtab *s, struct policy_file
1321
1345
goto bad ;
1322
1346
comdatum -> permissions .nprim = le32_to_cpu (buf [2 ]);
1323
1347
1324
- rc = str_read (& key , GFP_KERNEL , fp , len );
1348
+ rc = str_read_class (& key , GFP_KERNEL , fp , len );
1325
1349
if (rc )
1326
1350
goto bad ;
1327
1351
@@ -1559,12 +1583,12 @@ static int class_read(struct policydb *p, struct symtab *s, struct policy_file *
1559
1583
1560
1584
ncons = le32_to_cpu (buf [5 ]);
1561
1585
1562
- rc = str_read (& key , GFP_KERNEL , fp , len );
1586
+ rc = str_read_class (& key , GFP_KERNEL , fp , len );
1563
1587
if (rc )
1564
1588
goto bad ;
1565
1589
1566
1590
if (len2 ) {
1567
- rc = str_read (& cladatum -> comkey , GFP_KERNEL , fp , len2 );
1591
+ rc = str_read_class (& cladatum -> comkey , GFP_KERNEL , fp , len2 );
1568
1592
if (rc )
1569
1593
goto bad ;
1570
1594
@@ -1698,7 +1722,7 @@ static int role_read(struct policydb *p, struct symtab *s, struct policy_file *f
1698
1722
if (p -> policyvers >= POLICYDB_VERSION_BOUNDARY )
1699
1723
role -> bounds = le32_to_cpu (buf [2 ]);
1700
1724
1701
- rc = str_read (& key , GFP_KERNEL , fp , len );
1725
+ rc = str_read_role (& key , GFP_KERNEL , fp , len );
1702
1726
if (rc )
1703
1727
goto bad ;
1704
1728
@@ -1765,7 +1789,7 @@ static int type_read(struct policydb *p, struct symtab *s, struct policy_file *f
1765
1789
typdatum -> primary = le32_to_cpu (buf [2 ]);
1766
1790
}
1767
1791
1768
- rc = str_read (& key , GFP_KERNEL , fp , len );
1792
+ rc = str_read_type (& key , GFP_KERNEL , fp , len );
1769
1793
if (rc )
1770
1794
goto bad ;
1771
1795
@@ -1829,7 +1853,7 @@ static int user_read(struct policydb *p, struct symtab *s, struct policy_file *f
1829
1853
if (p -> policyvers >= POLICYDB_VERSION_BOUNDARY )
1830
1854
usrdatum -> bounds = le32_to_cpu (buf [2 ]);
1831
1855
1832
- rc = str_read (& key , GFP_KERNEL , fp , len );
1856
+ rc = str_read_user (& key , GFP_KERNEL , fp , len );
1833
1857
if (rc )
1834
1858
goto bad ;
1835
1859
@@ -1878,7 +1902,7 @@ static int sens_read(struct policydb *p, struct symtab *s, struct policy_file *f
1878
1902
goto bad ;
1879
1903
levdatum -> isalias = val ;
1880
1904
1881
- rc = str_read (& key , GFP_KERNEL , fp , len );
1905
+ rc = str_read_sens (& key , GFP_KERNEL , fp , len );
1882
1906
if (rc )
1883
1907
goto bad ;
1884
1908
@@ -1921,7 +1945,7 @@ static int cat_read(struct policydb *p, struct symtab *s, struct policy_file *fp
1921
1945
goto bad ;
1922
1946
catdatum -> isalias = val ;
1923
1947
1924
- rc = str_read (& key , GFP_KERNEL , fp , len );
1948
+ rc = str_read_cat (& key , GFP_KERNEL , fp , len );
1925
1949
if (rc )
1926
1950
goto bad ;
1927
1951
@@ -2230,7 +2254,7 @@ static int filename_trans_read_helper_compat(struct policydb *p, struct policy_f
2230
2254
len = le32_to_cpu (buf [0 ]);
2231
2255
2232
2256
/* path component string */
2233
- rc = str_read (& name , GFP_KERNEL , fp , len );
2257
+ rc = str_read (& name , GFP_KERNEL , fp , len , STR_UNCONSTRAINT , FILETRANSKEY_NAME_MAXLENGTH );
2234
2258
if (rc )
2235
2259
return rc ;
2236
2260
@@ -2329,7 +2353,7 @@ static int filename_trans_read_helper(struct policydb *p, struct policy_file *fp
2329
2353
len = le32_to_cpu (buf [0 ]);
2330
2354
2331
2355
/* path component string */
2332
- rc = str_read (& name , GFP_KERNEL , fp , len );
2356
+ rc = str_read (& name , GFP_KERNEL , fp , len , STR_UNCONSTRAINT , FILETRANSKEY_NAME_MAXLENGTH );
2333
2357
if (rc )
2334
2358
return rc ;
2335
2359
@@ -2483,7 +2507,7 @@ static int genfs_read(struct policydb *p, struct policy_file *fp)
2483
2507
if (!newgenfs )
2484
2508
goto out ;
2485
2509
2486
- rc = str_read (& newgenfs -> fstype , GFP_KERNEL , fp , len );
2510
+ rc = str_read_fsname (& newgenfs -> fstype , GFP_KERNEL , fp , len );
2487
2511
if (rc )
2488
2512
goto out ;
2489
2513
@@ -2522,7 +2546,8 @@ static int genfs_read(struct policydb *p, struct policy_file *fp)
2522
2546
if (!newc )
2523
2547
goto out ;
2524
2548
2525
- rc = str_read (& newc -> u .name , GFP_KERNEL , fp , len );
2549
+ rc = str_read (& newc -> u .name , GFP_KERNEL , fp , len ,
2550
+ STR_UNCONSTRAINT , GENFS_PATH_MAXLENGTH );
2526
2551
if (rc )
2527
2552
goto out ;
2528
2553
@@ -2625,7 +2650,7 @@ static int ocontext_read(struct policydb *p,
2625
2650
goto out ;
2626
2651
len = le32_to_cpu (buf [0 ]);
2627
2652
2628
- rc = str_read (& c -> u .name , GFP_KERNEL , fp , len );
2653
+ rc = str_read_fsname (& c -> u .name , GFP_KERNEL , fp , len );
2629
2654
if (rc )
2630
2655
goto out ;
2631
2656
@@ -2693,7 +2718,7 @@ static int ocontext_read(struct policydb *p,
2693
2718
goto out ;
2694
2719
2695
2720
len = le32_to_cpu (buf [1 ]);
2696
- rc = str_read (& c -> u .name , GFP_KERNEL , fp , len );
2721
+ rc = str_read_fsname (& c -> u .name , GFP_KERNEL , fp , len );
2697
2722
if (rc )
2698
2723
goto out ;
2699
2724
@@ -2759,7 +2784,9 @@ static int ocontext_read(struct policydb *p,
2759
2784
len = le32_to_cpu (buf [0 ]);
2760
2785
2761
2786
rc = str_read (& c -> u .ibendport .dev_name ,
2762
- GFP_KERNEL , fp , len );
2787
+ GFP_KERNEL , fp , len ,
2788
+ STR_UNCONSTRAINT ,
2789
+ INFINIBAND_DEVNAME_MAXLENGTH );
2763
2790
if (rc )
2764
2791
goto out ;
2765
2792
@@ -2827,7 +2854,8 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
2827
2854
goto bad ;
2828
2855
}
2829
2856
2830
- rc = str_read (& policydb_str , GFP_KERNEL , fp , len );
2857
+ rc = str_read (& policydb_str , GFP_KERNEL , fp , len ,
2858
+ STR_UNCONSTRAINT , strlen (POLICYDB_STRING ));
2831
2859
if (rc ) {
2832
2860
if (rc == - ENOMEM ) {
2833
2861
pr_err ("SELinux: unable to allocate memory for policydb string of length %d\n" ,
0 commit comments