@@ -1343,6 +1343,59 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
1343
1343
return string (buf , end , uuid , spec );
1344
1344
}
1345
1345
1346
+ int kptr_restrict __read_mostly ;
1347
+
1348
+ static noinline_for_stack
1349
+ char * restricted_pointer (char * buf , char * end , const void * ptr ,
1350
+ struct printf_spec spec )
1351
+ {
1352
+ spec .base = 16 ;
1353
+ spec .flags |= SMALL ;
1354
+ if (spec .field_width == -1 ) {
1355
+ spec .field_width = 2 * sizeof (ptr );
1356
+ spec .flags |= ZEROPAD ;
1357
+ }
1358
+
1359
+ switch (kptr_restrict ) {
1360
+ case 0 :
1361
+ /* Always print %pK values */
1362
+ break ;
1363
+ case 1 : {
1364
+ const struct cred * cred ;
1365
+
1366
+ /*
1367
+ * kptr_restrict==1 cannot be used in IRQ context
1368
+ * because its test for CAP_SYSLOG would be meaningless.
1369
+ */
1370
+ if (in_irq () || in_serving_softirq () || in_nmi ())
1371
+ return string (buf , end , "pK-error" , spec );
1372
+
1373
+ /*
1374
+ * Only print the real pointer value if the current
1375
+ * process has CAP_SYSLOG and is running with the
1376
+ * same credentials it started with. This is because
1377
+ * access to files is checked at open() time, but %pK
1378
+ * checks permission at read() time. We don't want to
1379
+ * leak pointer values if a binary opens a file using
1380
+ * %pK and then elevates privileges before reading it.
1381
+ */
1382
+ cred = current_cred ();
1383
+ if (!has_capability_noaudit (current , CAP_SYSLOG ) ||
1384
+ !uid_eq (cred -> euid , cred -> uid ) ||
1385
+ !gid_eq (cred -> egid , cred -> gid ))
1386
+ ptr = NULL ;
1387
+ break ;
1388
+ }
1389
+ case 2 :
1390
+ default :
1391
+ /* Always print 0's for %pK */
1392
+ ptr = NULL ;
1393
+ break ;
1394
+ }
1395
+
1396
+ return number (buf , end , (unsigned long )ptr , spec );
1397
+ }
1398
+
1346
1399
static noinline_for_stack
1347
1400
char * netdev_bits (char * buf , char * end , const void * addr , const char * fmt )
1348
1401
{
@@ -1591,8 +1644,6 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
1591
1644
return widen_string (buf , buf - buf_start , end , spec );
1592
1645
}
1593
1646
1594
- int kptr_restrict __read_mostly ;
1595
-
1596
1647
/*
1597
1648
* Show a '%p' thing. A kernel extension is that the '%p' is followed
1598
1649
* by an extra set of alphanumeric characters that are extended format
@@ -1792,47 +1843,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1792
1843
return buf ;
1793
1844
}
1794
1845
case 'K' :
1795
- switch (kptr_restrict ) {
1796
- case 0 :
1797
- /* Always print %pK values */
1798
- break ;
1799
- case 1 : {
1800
- const struct cred * cred ;
1801
-
1802
- /*
1803
- * kptr_restrict==1 cannot be used in IRQ context
1804
- * because its test for CAP_SYSLOG would be meaningless.
1805
- */
1806
- if (in_irq () || in_serving_softirq () || in_nmi ()) {
1807
- if (spec .field_width == -1 )
1808
- spec .field_width = default_width ;
1809
- return string (buf , end , "pK-error" , spec );
1810
- }
1811
-
1812
- /*
1813
- * Only print the real pointer value if the current
1814
- * process has CAP_SYSLOG and is running with the
1815
- * same credentials it started with. This is because
1816
- * access to files is checked at open() time, but %pK
1817
- * checks permission at read() time. We don't want to
1818
- * leak pointer values if a binary opens a file using
1819
- * %pK and then elevates privileges before reading it.
1820
- */
1821
- cred = current_cred ();
1822
- if (!has_capability_noaudit (current , CAP_SYSLOG ) ||
1823
- !uid_eq (cred -> euid , cred -> uid ) ||
1824
- !gid_eq (cred -> egid , cred -> gid ))
1825
- ptr = NULL ;
1826
- break ;
1827
- }
1828
- case 2 :
1829
- default :
1830
- /* Always print 0's for %pK */
1831
- ptr = NULL ;
1832
- break ;
1833
- }
1834
- break ;
1835
-
1846
+ return restricted_pointer (buf , end , ptr , spec );
1836
1847
case 'N' :
1837
1848
return netdev_bits (buf , end , ptr , fmt );
1838
1849
case 'a' :
0 commit comments