Skip to content

Commit 71338aa

Browse files
Dan Rosenbergdavem330
authored andcommitted
net: convert %p usage to %pK
The %pK format specifier is designed to hide exposed kernel pointers, specifically via /proc interfaces. Exposing these pointers provides an easy target for kernel write vulnerabilities, since they reveal the locations of writable structures containing easily triggerable function pointers. The behavior of %pK depends on the kptr_restrict sysctl. If kptr_restrict is set to 0, no deviation from the standard %p behavior occurs. If kptr_restrict is set to 1, the default, if the current user (intended to be a reader via seq_printf(), etc.) does not have CAP_SYSLOG (currently in the LSM tree), kernel pointers using %pK are printed as 0's. If kptr_restrict is set to 2, kernel pointers using %pK are printed as 0's regardless of privileges. Replacing with 0's was chosen over the default "(null)", which cannot be parsed by userland %p, which expects "(nil)". The supporting code for kptr_restrict and %pK are currently in the -mm tree. This patch converts users of %p in net/ to %pK. Cases of printing pointers to the syslog are not covered, since this would eliminate useful information for postmortem debugging and the reading of the syslog is already optionally protected by the dmesg_restrict sysctl. Signed-off-by: Dan Rosenberg <drosenberg@vsecurity.com> Cc: James Morris <jmorris@namei.org> Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Thomas Graf <tgraf@infradead.org> Cc: Eugene Teo <eugeneteo@kernel.org> Cc: Kees Cook <kees.cook@canonical.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: David S. Miller <davem@davemloft.net> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Eric Paris <eparis@parisplace.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 229de61 commit 71338aa

14 files changed

Lines changed: 22 additions & 22 deletions

File tree

‎net/atm/proc.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc)
191191
{
192192
struct sock *sk = sk_atm(vcc);
193193

194-
seq_printf(seq, "%p ", vcc);
194+
seq_printf(seq, "%pK ", vcc);
195195
if (!vcc->dev)
196196
seq_printf(seq, "Unassigned ");
197197
else
@@ -218,7 +218,7 @@ static void svc_info(struct seq_file *seq, struct atm_vcc *vcc)
218218
{
219219
if (!vcc->dev)
220220
seq_printf(seq, sizeof(void *) == 4 ?
221-
"N/A@%p%10s" : "N/A@%p%2s", vcc, "");
221+
"N/A@%pK%10s" : "N/A@%pK%2s", vcc, "");
222222
else
223223
seq_printf(seq, "%3d %3d %5d ",
224224
vcc->dev->number, vcc->vpi, vcc->vci);

‎net/can/bcm.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ static int bcm_proc_show(struct seq_file *m, void *v)
165165
struct bcm_sock *bo = bcm_sk(sk);
166166
struct bcm_op *op;
167167

168-
seq_printf(m, ">>> socket %p", sk->sk_socket);
169-
seq_printf(m, " / sk %p", sk);
170-
seq_printf(m, " / bo %p", bo);
168+
seq_printf(m, ">>> socket %pK", sk->sk_socket);
169+
seq_printf(m, " / sk %pK", sk);
170+
seq_printf(m, " / bo %pK", bo);
171171
seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
172172
seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
173173
seq_printf(m, " <<<\n");

‎net/ipv4/raw.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
979979
srcp = inet->inet_num;
980980

981981
seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
982-
" %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
982+
" %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
983983
i, src, srcp, dest, destp, sp->sk_state,
984984
sk_wmem_alloc_get(sp),
985985
sk_rmem_alloc_get(sp),

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,7 @@ static void get_openreq4(struct sock *sk, struct request_sock *req,
23712371
int ttd = req->expires - jiffies;
23722372

23732373
seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2374-
" %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p%n",
2374+
" %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %pK%n",
23752375
i,
23762376
ireq->loc_addr,
23772377
ntohs(inet_sk(sk)->inet_sport),
@@ -2426,7 +2426,7 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
24262426
rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
24272427

24282428
seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
2429-
"%08X %5d %8d %lu %d %p %lu %lu %u %u %d%n",
2429+
"%08X %5d %8d %lu %d %pK %lu %lu %u %u %d%n",
24302430
i, src, srcp, dest, destp, sk->sk_state,
24312431
tp->write_seq - tp->snd_una,
24322432
rx_queue,
@@ -2461,7 +2461,7 @@ static void get_timewait4_sock(struct inet_timewait_sock *tw,
24612461
srcp = ntohs(tw->tw_sport);
24622462

24632463
seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2464-
" %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p%n",
2464+
" %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK%n",
24652465
i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
24662466
3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
24672467
atomic_read(&tw->tw_refcnt), tw, len);

‎net/ipv4/udp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ static void udp4_format_sock(struct sock *sp, struct seq_file *f,
20902090
__u16 srcp = ntohs(inet->inet_sport);
20912091

20922092
seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2093-
" %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
2093+
" %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
20942094
bucket, src, srcp, dest, destp, sp->sk_state,
20952095
sk_wmem_alloc_get(sp),
20962096
sk_rmem_alloc_get(sp),

‎net/ipv6/raw.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
12401240
srcp = inet_sk(sp)->inet_num;
12411241
seq_printf(seq,
12421242
"%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1243-
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1243+
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
12441244
i,
12451245
src->s6_addr32[0], src->s6_addr32[1],
12461246
src->s6_addr32[2], src->s6_addr32[3], srcp,

‎net/ipv6/tcp_ipv6.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,7 @@ static void get_openreq6(struct seq_file *seq,
20362036

20372037
seq_printf(seq,
20382038
"%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2039-
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p\n",
2039+
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
20402040
i,
20412041
src->s6_addr32[0], src->s6_addr32[1],
20422042
src->s6_addr32[2], src->s6_addr32[3],
@@ -2087,7 +2087,7 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
20872087

20882088
seq_printf(seq,
20892089
"%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2090-
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %lu %lu %u %u %d\n",
2090+
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %lu %lu %u %u %d\n",
20912091
i,
20922092
src->s6_addr32[0], src->s6_addr32[1],
20932093
src->s6_addr32[2], src->s6_addr32[3], srcp,
@@ -2129,7 +2129,7 @@ static void get_timewait6_sock(struct seq_file *seq,
21292129

21302130
seq_printf(seq,
21312131
"%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2132-
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p\n",
2132+
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
21332133
i,
21342134
src->s6_addr32[0], src->s6_addr32[1],
21352135
src->s6_addr32[2], src->s6_addr32[3], srcp,

‎net/ipv6/udp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket
13911391
srcp = ntohs(inet->inet_sport);
13921392
seq_printf(seq,
13931393
"%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1394-
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1394+
"%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
13951395
bucket,
13961396
src->s6_addr32[0], src->s6_addr32[1],
13971397
src->s6_addr32[2], src->s6_addr32[3], srcp,

‎net/key/af_key.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3656,7 +3656,7 @@ static int pfkey_seq_show(struct seq_file *f, void *v)
36563656
if (v == SEQ_START_TOKEN)
36573657
seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
36583658
else
3659-
seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
3659+
seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
36603660
s,
36613661
atomic_read(&s->sk_refcnt),
36623662
sk_rmem_alloc_get(s),

‎net/netlink/af_netlink.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,7 @@ static int netlink_seq_show(struct seq_file *seq, void *v)
19851985
struct sock *s = v;
19861986
struct netlink_sock *nlk = nlk_sk(s);
19871987

1988-
seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %-8d %-8d %-8lu\n",
1988+
seq_printf(seq, "%pK %-3d %-6d %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
19891989
s,
19901990
s->sk_protocol,
19911991
nlk->pid,

0 commit comments

Comments
 (0)