Skip to content

Commit dc6c6fb

Browse files
committed
SUNRPC: Fix sockaddr handling in the svc_xprt_create_error trace point
While testing, I got an unexpected KASAN splat: Jan 08 13:50:27 oracle-102.nfsv4.dev kernel: BUG: KASAN: stack-out-of-bounds in trace_event_raw_event_svc_xprt_create_err+0x190/0x210 [sunrpc] Jan 08 13:50:27 oracle-102.nfsv4.dev kernel: Read of size 28 at addr ffffc9000008f728 by task mount.nfs/4628 The memcpy() in the TP_fast_assign section of this trace point copies the size of the destination buffer in order that the buffer won't be overrun. In other similar trace points, the source buffer for this memcpy is a "struct sockaddr_storage" so the actual length of the source buffer is always long enough to prevent the memcpy from reading uninitialized or unallocated memory. However, for this trace point, the source buffer can be as small as a "struct sockaddr_in". For AF_INET sockaddrs, the memcpy() reads memory that follows the source buffer, which is not always valid memory. To avoid copying past the end of the passed-in sockaddr, make the source address's length available to the memcpy(). It would be a little nicer if the tracing infrastructure was more friendly about storing socket addresses that are not AF_INET, but I could not find a way to make printk("%pIS") work with a dynamic array. Reported-by: KASAN Fixes: 4b8f380 ("SUNRPC: Tracepoint to record errors in svc_xpo_create()") Signed-off-by: Chuck Lever <[email protected]>
1 parent 0ea9fc1 commit dc6c6fb

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

include/trace/events/sunrpc.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,10 +1744,11 @@ TRACE_EVENT(svc_xprt_create_err,
17441744
const char *program,
17451745
const char *protocol,
17461746
struct sockaddr *sap,
1747+
size_t salen,
17471748
const struct svc_xprt *xprt
17481749
),
17491750

1750-
TP_ARGS(program, protocol, sap, xprt),
1751+
TP_ARGS(program, protocol, sap, salen, xprt),
17511752

17521753
TP_STRUCT__entry(
17531754
__field(long, error)
@@ -1760,7 +1761,7 @@ TRACE_EVENT(svc_xprt_create_err,
17601761
__entry->error = PTR_ERR(xprt);
17611762
__assign_str(program, program);
17621763
__assign_str(protocol, protocol);
1763-
memcpy(__entry->addr, sap, sizeof(__entry->addr));
1764+
memcpy(__entry->addr, sap, min(salen, sizeof(__entry->addr)));
17641765
),
17651766

17661767
TP_printk("addr=%pISpc program=%s protocol=%s error=%ld",

net/sunrpc/svc_xprt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl,
243243
xprt = xcl->xcl_ops->xpo_create(serv, net, sap, len, flags);
244244
if (IS_ERR(xprt))
245245
trace_svc_xprt_create_err(serv->sv_program->pg_name,
246-
xcl->xcl_name, sap, xprt);
246+
xcl->xcl_name, sap, len, xprt);
247247
return xprt;
248248
}
249249

0 commit comments

Comments
 (0)