Skip to content

Commit fe163e5

Browse files
ebiggersdavem330
authored andcommitted
isdn/capi: check message length in capi_write()
syzbot reported: BUG: KMSAN: uninit-value in capi_write+0x791/0xa90 drivers/isdn/capi/capi.c:700 CPU: 0 PID: 10025 Comm: syz-executor379 Not tainted 4.20.0-rc7+ #2 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0x173/0x1d0 lib/dump_stack.c:113 kmsan_report+0x12e/0x2a0 mm/kmsan/kmsan.c:613 __msan_warning+0x82/0xf0 mm/kmsan/kmsan_instr.c:313 capi_write+0x791/0xa90 drivers/isdn/capi/capi.c:700 do_loop_readv_writev fs/read_write.c:703 [inline] do_iter_write+0x83e/0xd80 fs/read_write.c:961 vfs_writev fs/read_write.c:1004 [inline] do_writev+0x397/0x840 fs/read_write.c:1039 __do_sys_writev fs/read_write.c:1112 [inline] __se_sys_writev+0x9b/0xb0 fs/read_write.c:1109 __x64_sys_writev+0x4a/0x70 fs/read_write.c:1109 do_syscall_64+0xbc/0xf0 arch/x86/entry/common.c:291 entry_SYSCALL_64_after_hwframe+0x63/0xe7 [...] The problem is that capi_write() is reading past the end of the message. Fix it by checking the message's length in the needed places. Reported-and-tested-by: [email protected] Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1c2977c commit fe163e5

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

drivers/isdn/capi/capi.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos
688688
if (!cdev->ap.applid)
689689
return -ENODEV;
690690

691+
if (count < CAPIMSG_BASELEN)
692+
return -EINVAL;
693+
691694
skb = alloc_skb(count, GFP_USER);
692695
if (!skb)
693696
return -ENOMEM;
@@ -698,7 +701,8 @@ capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos
698701
}
699702
mlen = CAPIMSG_LEN(skb->data);
700703
if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
701-
if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
704+
if (count < CAPI_DATA_B3_REQ_LEN ||
705+
(size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
702706
kfree_skb(skb);
703707
return -EINVAL;
704708
}
@@ -711,6 +715,10 @@ capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos
711715
CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
712716

713717
if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
718+
if (count < CAPI_DISCONNECT_B3_RESP_LEN) {
719+
kfree_skb(skb);
720+
return -EINVAL;
721+
}
714722
mutex_lock(&cdev->lock);
715723
capincci_free(cdev, CAPIMSG_NCCI(skb->data));
716724
mutex_unlock(&cdev->lock);

include/uapi/linux/isdn/capicmd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define CAPI_MSG_BASELEN 8
1717
#define CAPI_DATA_B3_REQ_LEN (CAPI_MSG_BASELEN+4+4+2+2+2)
1818
#define CAPI_DATA_B3_RESP_LEN (CAPI_MSG_BASELEN+4+2)
19+
#define CAPI_DISCONNECT_B3_RESP_LEN (CAPI_MSG_BASELEN+4)
1920

2021
/*----- CAPI commands -----*/
2122
#define CAPI_ALERT 0x01

0 commit comments

Comments
 (0)