From patchwork Mon Jul 12 19:55:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 12372121 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C090C07E9A for ; Mon, 12 Jul 2021 19:56:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E82FA61248 for ; Mon, 12 Jul 2021 19:56:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236670AbhGLT7E (ORCPT ); Mon, 12 Jul 2021 15:59:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236686AbhGLT7B (ORCPT ); Mon, 12 Jul 2021 15:59:01 -0400 Received: from mail-io1-xd2e.google.com (mail-io1-xd2e.google.com [IPv6:2607:f8b0:4864:20::d2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D457C0613DD; Mon, 12 Jul 2021 12:56:09 -0700 (PDT) Received: by mail-io1-xd2e.google.com with SMTP id y16so1118339iol.12; Mon, 12 Jul 2021 12:56:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0cpcyWJ2zEX/TjiKs0DRqIAiPNZkNue7/VjGo7bfHvk=; b=ZGthQaNgVzHKU1vcY/aTWjuMr/8v44yT549mfft30wXJYAmgX0p3Gqn13SP9IEb+HT 3FnkFZNEAL7yG1VI/QzKOW83rPWmgZcSwnV8otewq6BO7RfgGOvdMy8QTzAZPDll1NM+ RFFYZo6hWR1itbeORncCnzoXbwq77VvmYAufj1PmKND1DrZauj5xW6XXdBdn7Lm3mLNa 5iR3L1qJ55V4Ssm8MNcha/5Wb6+AS7uzhNzDUMhL4xJZ/ydUZKokoYE+nJfisr+L6kWq GfwFzGVCDERMk4sgz3oWNUfKOjShCeFY8HEnkKFq7xcKOi0Iw0wqAfHs8F3+QytPlNeh mWnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0cpcyWJ2zEX/TjiKs0DRqIAiPNZkNue7/VjGo7bfHvk=; b=NJsZivW1qlSH9nHwUxMXCm++aTF3KotvskRE5livSA9/cg0QW4L2giO6vybT2LsrLE /he3OBAexSdfE0HAUqUeG1p157vBIgzioJ4LLqwyQ0Azhbss2EO9r2FaDBrc/UyaAvnO s6vqT5w/zbl6gj/hLHv3rxrJ40qWGOMSYKPfAIVQq+AgTm6u0cK4HNBaEKYvOsRTS7yT HtxJEyobVRnh7DLX1KzaawCZkQgsKHs+vKUY1bulwUSQkSR+7bDeINuxwQQ4l7+O+yuC hSsgCLmU0/n6lilCrrCE563xcOUQFuJbSKa7Y8LLqYBRJqFNGD9VGcruPa+7FUC33wAM ZUgA== X-Gm-Message-State: AOAM531pHhyUpmFfj5K8JOZRdMKMfkG2p2M6MXM3SFrpr4BcsjMMPZTi SbadwCgoG7KVolTzCK8StQw= X-Google-Smtp-Source: ABdhPJyQ74iNCOb74ZeV7ltve5c3BH1maZHaaaNCE/2y7Nmu41zbs83V9w/wI6TIlmmsyJL7HmMPDQ== X-Received: by 2002:a02:c95a:: with SMTP id u26mr603377jao.49.1626119768895; Mon, 12 Jul 2021 12:56:08 -0700 (PDT) Received: from john-XPS-13-9370.lan ([172.243.157.240]) by smtp.gmail.com with ESMTPSA id l5sm8389210ion.44.2021.07.12.12.56.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jul 2021 12:56:08 -0700 (PDT) From: John Fastabend To: ast@kernel.org, jakub@cloudflare.com, daniel@iogearbox.net, andriin@fb.com, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, john.fastabend@gmail.com Subject: [PATCH bpf v4 1/2] bpf, sockmap: fix potential memory leak on unlikely error case Date: Mon, 12 Jul 2021 12:55:45 -0700 Message-Id: <20210712195546.423990-2-john.fastabend@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210712195546.423990-1-john.fastabend@gmail.com> References: <20210712195546.423990-1-john.fastabend@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net If skb_linearize is needed and fails we could leak a msg on the error handling. To fix ensure we kfree the msg block before returning error. Found during code review. Fixes: 4363023d2668e ("bpf, sockmap: Avoid failures from skb_to_sgvec when skb has frag_list") Signed-off-by: John Fastabend Reviewed-by: Cong Wang --- net/core/skmsg.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 9b6160a191f8..15d71288e741 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -508,10 +508,8 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb, if (skb_linearize(skb)) return -EAGAIN; num_sge = skb_to_sgvec(skb, msg->sg.data, 0, skb->len); - if (unlikely(num_sge < 0)) { - kfree(msg); + if (unlikely(num_sge < 0)) return num_sge; - } copied = skb->len; msg->sg.start = 0; @@ -530,6 +528,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb) { struct sock *sk = psock->sk; struct sk_msg *msg; + int err; /* If we are receiving on the same sock skb->sk is already assigned, * skip memory accounting and owner transition seeing it already set @@ -548,7 +547,10 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb) * into user buffers. */ skb_set_owner_r(skb, sk); - return sk_psock_skb_ingress_enqueue(skb, psock, sk, msg); + err = sk_psock_skb_ingress_enqueue(skb, psock, sk, msg); + if (err < 0) + kfree(msg); + return err; } /* Puts an skb on the ingress queue of the socket already assigned to the @@ -559,12 +561,16 @@ static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb { struct sk_msg *msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_ATOMIC); struct sock *sk = psock->sk; + int err; if (unlikely(!msg)) return -EAGAIN; sk_msg_init(msg); skb_set_owner_r(skb, sk); - return sk_psock_skb_ingress_enqueue(skb, psock, sk, msg); + err = sk_psock_skb_ingress_enqueue(skb, psock, sk, msg); + if (err < 0) + kfree(msg); + return err; } static int sk_psock_handle_skb(struct sk_psock *psock, struct sk_buff *skb, From patchwork Mon Jul 12 19:55:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 12372123 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C80DC11F67 for ; Mon, 12 Jul 2021 19:56:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 64CDD61245 for ; Mon, 12 Jul 2021 19:56:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235846AbhGLT7M (ORCPT ); Mon, 12 Jul 2021 15:59:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236711AbhGLT7G (ORCPT ); Mon, 12 Jul 2021 15:59:06 -0400 Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29F3AC0613DD; Mon, 12 Jul 2021 12:56:17 -0700 (PDT) Received: by mail-il1-x134.google.com with SMTP id o8so10031418ilf.4; Mon, 12 Jul 2021 12:56:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=A+PcvZFpIdycnLsj6soRgU5MzyOWEpr7KT3V/KooJ6o=; b=nxV4HxKQq5Z7Bwj4ef26X9sp3iR0xAo0Q8y9mhx4CvNu7hz6MpnX0R2XRjSrJTQEqG ppmC+cW6rc8IdYGDMyvJt5TU/2KxDdoA7+z9E9Cj5BAm8ytW4IMuV15VCw4/3Y/pgZlb eOyvmTSHcPquvvcPejpuxfrMEk8Bq/oXRys8RqjTfpU/lLmILLOvL6E/VmzLvS/2LMoJ 3HhDqcFmHFW47JTLXefOUxNVd4Gu+lwwt6PD9rjiHhsiy39DdN9gtK4fDIaoLxL9kd+2 VhlLeEi9y7ezBBgnN4hgRhPFvhaJOeyJBHOr3TnI2ec5YmPKvs9vWSYhDsIa9O4wZ9NQ 4XuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=A+PcvZFpIdycnLsj6soRgU5MzyOWEpr7KT3V/KooJ6o=; b=q5H2gORANDp9fLuMXOiLL/Y+wJkaN2letzPPWJ+VhqYZz0lSrc3gf+hB8CloRMdqLJ 1M5cFN80g/s4KraRwsw5E8hi0NjMEvu3bUeqpvNxqmhDdLKUORtJIH8SEia0Qh7cA+dU kh6D0Wa5014olPGRbVSUWwWOWBpqOVfcpvXYiOw3xCEBPDytnWVQTg3QdCyELsiNZDXf EqQI0HeJqodOTJE555+SVp3Cw0BO/lGoiMogbDmox4dHlhULz8gmXDdJUqpwJjv6FxE0 Hxtl852THN68wLrEnUir2Qu06abXe8ie59RCvkpbtvv4xCzCggkj+mXs/8quQc3z31Gm 5Wdw== X-Gm-Message-State: AOAM532+v9/ELwS+TCi/2akQgsf04fa+9WM7C+Wlp6fiSawD217q5R4Z CwW91HmMuG+3ibu4CKhvDac= X-Google-Smtp-Source: ABdhPJw6qQ4+a4FCn958FQpKKid/Hq4o0QHRVYmlmPgl/rwnfl547DZmLtZgpNnKl7bf8DUla8FAWA== X-Received: by 2002:a92:d9c6:: with SMTP id n6mr388331ilq.142.1626119776627; Mon, 12 Jul 2021 12:56:16 -0700 (PDT) Received: from john-XPS-13-9370.lan ([172.243.157.240]) by smtp.gmail.com with ESMTPSA id l5sm8389210ion.44.2021.07.12.12.56.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jul 2021 12:56:16 -0700 (PDT) From: John Fastabend To: ast@kernel.org, jakub@cloudflare.com, daniel@iogearbox.net, andriin@fb.com, xiyou.wangcong@gmail.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, john.fastabend@gmail.com Subject: [PATCH bpf v4 2/2] bpf, sockmap: sk_prot needs inuse_idx set for proc stats Date: Mon, 12 Jul 2021 12:55:46 -0700 Message-Id: <20210712195546.423990-3-john.fastabend@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210712195546.423990-1-john.fastabend@gmail.com> References: <20210712195546.423990-1-john.fastabend@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Proc socket stats use sk_prot->inuse_idx value to record inuse sock stats. We currently do not set this correctly from sockmap side. The result is reading sock stats '/proc/net/sockstat' gives incorrect values. The socket counter is incremented correctly, but because we don't set the counter correctly when we replace sk_prot we may omit the decrement. To get the correct inuse_idx value move the core_initcall that initializes the tcp/udp proto handlers to late_initcall. This way it is initialized after TCP/UDP has the chance to assign the inuse_idx value from the register protocol handler. Suggested-by: Jakub Sitnicki Fixes: 604326b41a6fb ("bpf, sockmap: convert to generic sk_msg interface") Signed-off-by: John Fastabend Reviewed-by: Cong Wang --- net/ipv4/tcp_bpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index f26916a62f25..d3e9386b493e 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -503,7 +503,7 @@ static int __init tcp_bpf_v4_build_proto(void) tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot); return 0; } -core_initcall(tcp_bpf_v4_build_proto); +late_initcall(tcp_bpf_v4_build_proto); static int tcp_bpf_assert_proto_ops(struct proto *ops) {