Skip to content

Commit 57e7344

Browse files
committed
vsprintf: refactor %pK code out of pointer()
Currently code to handle %pK is all within the switch statement in pointer(). This is the wrong level of abstraction. Each of the other switch clauses call a helper function, pK should do the same. Refactor code out of pointer() to new function restricted_pointer(). Signed-off-by: Tobin C. Harding <[email protected]>
1 parent 553d8e8 commit 57e7344

File tree

1 file changed

+54
-43
lines changed

1 file changed

+54
-43
lines changed

lib/vsprintf.c

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,59 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
13431343
return string(buf, end, uuid, spec);
13441344
}
13451345

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+
13461399
static noinline_for_stack
13471400
char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
13481401
{
@@ -1591,8 +1644,6 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
15911644
return widen_string(buf, buf - buf_start, end, spec);
15921645
}
15931646

1594-
int kptr_restrict __read_mostly;
1595-
15961647
/*
15971648
* Show a '%p' thing. A kernel extension is that the '%p' is followed
15981649
* 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,
17921843
return buf;
17931844
}
17941845
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);
18361847
case 'N':
18371848
return netdev_bits(buf, end, ptr, fmt);
18381849
case 'a':

0 commit comments

Comments
 (0)