|
| 1 | +netns: Make get_net_ns() handle zero refcount net |
| 2 | + |
| 3 | +jira LE-3201 |
| 4 | +cve CVE-2024-40958 |
| 5 | +Rebuild_History Non-Buildable kernel-rt-4.18.0-553.22.1.rt7.363.el8_10 |
| 6 | +commit-author Yue Haibing <yuehaibing@huawei.com> |
| 7 | +commit ff960f9d3edbe08a736b5a224d91a305ccc946b0 |
| 8 | +Empty-Commit: Cherry-Pick Conflicts during history rebuild. |
| 9 | +Will be included in final tarball splat. Ref for failed cherry-pick at: |
| 10 | +ciq/ciq_backports/kernel-rt-4.18.0-553.22.1.rt7.363.el8_10/ff960f9d.failed |
| 11 | + |
| 12 | +Syzkaller hit a warning: |
| 13 | +refcount_t: addition on 0; use-after-free. |
| 14 | +WARNING: CPU: 3 PID: 7890 at lib/refcount.c:25 refcount_warn_saturate+0xdf/0x1d0 |
| 15 | +Modules linked in: |
| 16 | +CPU: 3 PID: 7890 Comm: tun Not tainted 6.10.0-rc3-00100-gcaa4f9578aba-dirty #310 |
| 17 | +Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014 |
| 18 | +RIP: 0010:refcount_warn_saturate+0xdf/0x1d0 |
| 19 | +Code: 41 49 04 31 ff 89 de e8 9f 1e cd fe 84 db 75 9c e8 76 26 cd fe c6 05 b6 41 49 04 01 90 48 c7 c7 b8 8e 25 86 e8 d2 05 b5 fe 90 <0f> 0b 90 90 e9 79 ff ff ff e8 53 26 cd fe 0f b6 1 |
| 20 | +RSP: 0018:ffff8881067b7da0 EFLAGS: 00010286 |
| 21 | +RAX: 0000000000000000 RBX: 0000000000000000 RCX: ffffffff811c72ac |
| 22 | +RDX: ffff8881026a2140 RSI: ffffffff811c72b5 RDI: 0000000000000001 |
| 23 | +RBP: ffff8881067b7db0 R08: 0000000000000000 R09: 205b5d3730353139 |
| 24 | +R10: 0000000000000000 R11: 205d303938375420 R12: ffff8881086500c4 |
| 25 | +R13: ffff8881086500c4 R14: ffff8881086500b0 R15: ffff888108650040 |
| 26 | +FS: 00007f5b2961a4c0(0000) GS:ffff88823bd00000(0000) knlGS:0000000000000000 |
| 27 | +CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 |
| 28 | +CR2: 000055d7ed36fd18 CR3: 00000001482f6000 CR4: 00000000000006f0 |
| 29 | +DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 |
| 30 | +DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 |
| 31 | +Call Trace: |
| 32 | + <TASK> |
| 33 | + ? show_regs+0xa3/0xc0 |
| 34 | + ? __warn+0xa5/0x1c0 |
| 35 | + ? refcount_warn_saturate+0xdf/0x1d0 |
| 36 | + ? report_bug+0x1fc/0x2d0 |
| 37 | + ? refcount_warn_saturate+0xdf/0x1d0 |
| 38 | + ? handle_bug+0xa1/0x110 |
| 39 | + ? exc_invalid_op+0x3c/0xb0 |
| 40 | + ? asm_exc_invalid_op+0x1f/0x30 |
| 41 | + ? __warn_printk+0xcc/0x140 |
| 42 | + ? __warn_printk+0xd5/0x140 |
| 43 | + ? refcount_warn_saturate+0xdf/0x1d0 |
| 44 | + get_net_ns+0xa4/0xc0 |
| 45 | + ? __pfx_get_net_ns+0x10/0x10 |
| 46 | + open_related_ns+0x5a/0x130 |
| 47 | + __tun_chr_ioctl+0x1616/0x2370 |
| 48 | + ? __sanitizer_cov_trace_switch+0x58/0xa0 |
| 49 | + ? __sanitizer_cov_trace_const_cmp2+0x1c/0x30 |
| 50 | + ? __pfx_tun_chr_ioctl+0x10/0x10 |
| 51 | + tun_chr_ioctl+0x2f/0x40 |
| 52 | + __x64_sys_ioctl+0x11b/0x160 |
| 53 | + x64_sys_call+0x1211/0x20d0 |
| 54 | + do_syscall_64+0x9e/0x1d0 |
| 55 | + entry_SYSCALL_64_after_hwframe+0x77/0x7f |
| 56 | +RIP: 0033:0x7f5b28f165d7 |
| 57 | +Code: b3 66 90 48 8b 05 b1 48 2d 00 64 c7 00 26 00 00 00 48 c7 c0 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 b8 10 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 81 48 2d 00 8 |
| 58 | +RSP: 002b:00007ffc2b59c5e8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010 |
| 59 | +RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f5b28f165d7 |
| 60 | +RDX: 0000000000000000 RSI: 00000000000054e3 RDI: 0000000000000003 |
| 61 | +RBP: 00007ffc2b59c650 R08: 00007f5b291ed8c0 R09: 00007f5b2961a4c0 |
| 62 | +R10: 0000000029690010 R11: 0000000000000246 R12: 0000000000400730 |
| 63 | +R13: 00007ffc2b59cf40 R14: 0000000000000000 R15: 0000000000000000 |
| 64 | + </TASK> |
| 65 | +Kernel panic - not syncing: kernel: panic_on_warn set ... |
| 66 | + |
| 67 | +This is trigger as below: |
| 68 | + ns0 ns1 |
| 69 | +tun_set_iff() //dev is tun0 |
| 70 | + tun->dev = dev |
| 71 | +//ip link set tun0 netns ns1 |
| 72 | + put_net() //ref is 0 |
| 73 | +__tun_chr_ioctl() //TUNGETDEVNETNS |
| 74 | + net = dev_net(tun->dev); |
| 75 | + open_related_ns(&net->ns, get_net_ns); //ns1 |
| 76 | + get_net_ns() |
| 77 | + get_net() //addition on 0 |
| 78 | + |
| 79 | +Use maybe_get_net() in get_net_ns in case net's ref is zero to fix this |
| 80 | + |
| 81 | +Fixes: 0c3e0e3bb623 ("tun: Add ioctl() TUNGETDEVNETNS cmd to allow obtaining real net ns of tun device") |
| 82 | + Signed-off-by: Yue Haibing <yuehaibing@huawei.com> |
| 83 | +Link: https://lore.kernel.org/r/20240614131302.2698509-1-yuehaibing@huawei.com |
| 84 | + Signed-off-by: Paolo Abeni <pabeni@redhat.com> |
| 85 | + |
| 86 | +(cherry picked from commit ff960f9d3edbe08a736b5a224d91a305ccc946b0) |
| 87 | + Signed-off-by: Jonathan Maple <jmaple@ciq.com> |
| 88 | + |
| 89 | +# Conflicts: |
| 90 | +# net/core/net_namespace.c |
| 91 | +diff --cc net/core/net_namespace.c |
| 92 | +index cde133cd2b10,6a823ba906c6..000000000000 |
| 93 | +--- a/net/core/net_namespace.c |
| 94 | ++++ b/net/core/net_namespace.c |
| 95 | +@@@ -635,33 -689,42 +635,53 @@@ void __put_net(struct net *net |
| 96 | + } |
| 97 | + EXPORT_SYMBOL_GPL(__put_net); |
| 98 | + |
| 99 | +++<<<<<<< HEAD |
| 100 | +++======= |
| 101 | ++ /** |
| 102 | ++ * get_net_ns - increment the refcount of the network namespace |
| 103 | ++ * @ns: common namespace (net) |
| 104 | ++ * |
| 105 | ++ * Returns the net's common namespace or ERR_PTR() if ref is zero. |
| 106 | ++ */ |
| 107 | ++ struct ns_common *get_net_ns(struct ns_common *ns) |
| 108 | ++ { |
| 109 | ++ struct net *net; |
| 110 | ++ |
| 111 | ++ net = maybe_get_net(container_of(ns, struct net, ns)); |
| 112 | ++ if (net) |
| 113 | ++ return &net->ns; |
| 114 | ++ return ERR_PTR(-EINVAL); |
| 115 | ++ } |
| 116 | ++ EXPORT_SYMBOL_GPL(get_net_ns); |
| 117 | ++ |
| 118 | +++>>>>>>> ff960f9d3edb (netns: Make get_net_ns() handle zero refcount net) |
| 119 | + struct net *get_net_ns_by_fd(int fd) |
| 120 | + { |
| 121 | + - struct fd f = fdget(fd); |
| 122 | + - struct net *net = ERR_PTR(-EINVAL); |
| 123 | + + struct file *file; |
| 124 | + + struct ns_common *ns; |
| 125 | + + struct net *net; |
| 126 | + |
| 127 | + - if (!f.file) |
| 128 | + - return ERR_PTR(-EBADF); |
| 129 | + + file = proc_ns_fget(fd); |
| 130 | + + if (IS_ERR(file)) |
| 131 | + + return ERR_CAST(file); |
| 132 | + |
| 133 | + - if (proc_ns_file(f.file)) { |
| 134 | + - struct ns_common *ns = get_proc_ns(file_inode(f.file)); |
| 135 | + - if (ns->ops == &netns_operations) |
| 136 | + - net = get_net(container_of(ns, struct net, ns)); |
| 137 | + - } |
| 138 | + - fdput(f); |
| 139 | + + ns = get_proc_ns(file_inode(file)); |
| 140 | + + if (ns->ops == &netns_operations) |
| 141 | + + net = get_net(container_of(ns, struct net, ns)); |
| 142 | + + else |
| 143 | + + net = ERR_PTR(-EINVAL); |
| 144 | + |
| 145 | + + fput(file); |
| 146 | + return net; |
| 147 | + } |
| 148 | + -EXPORT_SYMBOL_GPL(get_net_ns_by_fd); |
| 149 | + + |
| 150 | + +#else |
| 151 | + +struct net *get_net_ns_by_fd(int fd) |
| 152 | + +{ |
| 153 | + + return ERR_PTR(-EINVAL); |
| 154 | + +} |
| 155 | + #endif |
| 156 | + +EXPORT_SYMBOL_GPL(get_net_ns_by_fd); |
| 157 | + |
| 158 | + struct net *get_net_ns_by_pid(pid_t pid) |
| 159 | + { |
| 160 | +* Unmerged path net/core/net_namespace.c |
0 commit comments