On 30 Nov 17:15, netd…@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential > issue(s) to consider. > > Critical: 0 · High: 0 (1) · Medium: 1 · Low: 1 > > If you believe none of the issues are legitimate, please reply to each > issue of Medium severity or higher and explain why the AI is wrong. > If any of them are legitimate and you plan to respin, please let patchwork > know by including "pw-bot: cr" as a separate line at the end of your reply > (one such reply per series is enough). > > - [Medium] The new early return `if (!user_backed_iter(it)) return > ERR_PTR(-EOPNOTSUPP);` in tun_napi_alloc_frags() runs before… > - [Low] The commit message understates what the bug does. > > Pre-existing issues: > - [High] This problem wasn't introduced by this patch, but the same 'skb > lengths do not match the fragment layout' bug stays reachable with a… > > -- > > tun: reject iterators that are not user backed in NAPI frags mode > > This commit adds a user_backed_iter() check at the top of > tun_napi_alloc_frags(). Iterators that are not user backed, such as the > ITER_BVEC used by io_uring fixed-buffer writes, are now rejected with > -EOPNOTSUPP. Before this, the fragment loop read bio_vec entries as if > they were iovec entries. > > Link: https://lore.kernel.org/netdev/will…@gmail.com/ >
> > This can result in an SKB where the sum of the fragment sizes does > > not match skb->data_len. This was reproduced using an io_uring > > fixed-buffer write to a TAP device, see the reproducer code below.
> > [Severity: Low] > The code change itself is fine, but does this description understate > what the bug does? > > In tun_napi_alloc_frags(), bvec[i] is read as a struct iovec. On 64-bit > little-endian, iov_len overlays bv_len | (bv_offset << 32), so fragsz is > 4096 when bv_offset is 0: > > const struct iovec *iov = iter_iov(it) + i; > size_t fragsz = iov->iov_len; > ... > frag = netdev_alloc_frag(fragsz); > > netdev_alloc_frag() does not zero the memory. Later, tun_get_user() calls: > > err = skb_copy_datagram_from_iter(skb, 0, from, len); > > This fills only data_len bytes of the frags and returns 0 once len is > used up. The skb is then passed to napi_gro_frags(). > > With the reproducer output (len=5042, data_len=946, frag_bytes=4096), > 3150 bytes of stale page-frag memory sit inside a frag labelled 4096 > bytes. > > Could anything that walks frags by skb_frag_size() read or transmit that > memory? Two examples are an SG-capable NIC driver after bridging or > forwarding, and skb_segment(). > > If so, could the commit message mention the possible kernel memory > disclosure, so stable and security triage can weigh it properly?
I have reproduced only the length mismatch, I have not verified whether this can lead to a kernel memory disclosure. I'll mention the possibility of it in the v2's commit message.
> > diff --git a/drivers/net/tun.c b/drivers/net/tun.c > > index ae05b1e58abaa..864945f2fac20 100644 > > --- a/drivers/net/tun.c > > +++ b/drivers/net/tun.c > > @@ -1502,6 +1502,9 @@ static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, > > int err; > > int i; > > > > + if (!user_backed_iter(it)) > > + return ERR_PTR(-EOPNOTSUPP); > > +
> > [Severity: Medium] > Can this early return leak a recycled skb cached in tfile->napi.skb? > > The return happens before napi_get_frags(), and before the free label > that calls napi_free_frags(). > > tfile->napi.skb can still hold an skb from an earlier write. That > happens when napi_gro_frags() returns GRO_MERGED_FREE: > > net/core/gro.c:napi_frags_finish() { > ... > case GRO_MERGED_FREE: > if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) > napi_skb_free_stolen_head(skb); > else > napi_reuse_skb(napi, skb); > ... > } > > napi_reuse_skb() ends with napi->skb = skb, and tun_get_user() then > just unlocks napi_mutex. > > Now suppose the next write is an io_uring IORING_OP_WRITE_FIXED > (ITER_BVEC) to the same IFF_NAPI_FRAGS device. tun_get_user() jumps to > drop with an ERR_PTR skb: > > drivers/net/tun.c:tun_get_user() { > ... > free_skb: > drop_reason = drop_reason ?: SKB_DROP_REASON_NOT_SPECIFIED; > if (!IS_ERR_OR_NULL(skb)) > kfree_skb_reason(skb, drop_reason); > > unlock_frags: > if (frags) { > tfile->napi.skb = NULL; > mutex_unlock(&tfile->napi_mutex); > } > ... > } > > The ERR_PTR skb is not freed. Then tfile->napi.skb = NULL drops the > only reference to the recycled sk_buff and its head buffer. > napi_free_frags() at NAPI teardown cannot reclaim it either, because > napi->skb is already NULL. > > A write that GRO merges followed by a fixed-buffer write would then leak > one skb per pair, with no limit. Before this patch, the bvec write > reused the cached skb instead. > > The existing -EMSGSIZE early return just below has the same flaw. > However, this check adds an easy way to trigger it for a whole class of > iterators. > > Would it be better to do the user_backed_iter() check in tun_get_user() > before taking napi_mutex? Another option would be to clear > tfile->napi.skb in unlock_frags only when it was actually used or freed.
Instead of moving the check to tun_get_user(), we can do goto free in tun_napi_alloc_frags() if the initial check is false. napi_free_frags() is NULL safe therefore no issues arise. I'll do that for both user_backed_iter() and the existing -EMSGSIZE branches.
> > if (it->nr_segs > MAX_SKB_FRAGS + 1 || > > len > (ETH_MAX_MTU - NET_SKB_PAD - NET_IP_ALIGN)) > > return ERR_PTR(-EMSGSIZE);
> > [Severity: High] > This is a pre-existing issue and was not introduced by this patch. Can > the same mismatch between skb lengths and frag layout still be reached > with a user-backed ITER_IOVEC iterator? The new check lets those > through. > > When tun_get_user() is called with msg_control set (the vhost-net > zerocopy TX path, TUN_MSG_UBUF), zerocopy is true and copylen stays > short: > > copylen = min(hdr_len ? hdr_len : GOODCOPY_LEN, good_linear); > ... > if (frags) { > mutex_lock(&tfile->napi_mutex); > skb = tun_napi_alloc_frags(tfile, copylen, from); > ... > zerocopy = false; > ... > err = skb_copy_datagram_from_iter(skb, 0, from, len); > > Inside tun_napi_alloc_frags(), linear is the length of the first > segment, which the guest controls: > > linear = iov_iter_single_seg_count(it); > err = __skb_grow(skb, linear); > ... > skb->len = len; > skb->data_len = len - linear; > skb->truesize += skb->data_len; > > Suppose the first segment is longer than copylen, for example 1500 vs > 128. Then skb->data_len wraps to roughly 0xFFFFFAA4 and truesize wraps > as well, while skb->len is 128. > > Would the following eth_type_trans()->skb_pull_inline()->__skb_pull() > then see skb->len < skb->data_len and hit BUG()? > > If the first segment is shorter than copylen, the frag sizes exceed > data_len instead. That is the symptom this commit describes. > > It looks like the cause here is the length argument passed in, not the > iterator type. > > Reaching this needs an IFF_NAPI_FRAGS tap used as a vhost-net backend > with experimental_zcopytx=1. Once that is set up, the guest's TX > descriptor layout decides whether it triggers.
I'll look into this and send a new patch if needed. pw-bot: cr