From patchwork Fri Jul 2 00:11:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 469820 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 B81F7C11F6A for ; Fri, 2 Jul 2021 00:11:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9E02761410 for ; Fri, 2 Jul 2021 00:11:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234444AbhGBAOV (ORCPT ); Thu, 1 Jul 2021 20:14:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234195AbhGBAOU (ORCPT ); Thu, 1 Jul 2021 20:14:20 -0400 Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48C8EC061762; Thu, 1 Jul 2021 17:11:49 -0700 (PDT) Received: by mail-io1-xd2c.google.com with SMTP id k16so9650894ios.10; Thu, 01 Jul 2021 17:11:49 -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=SiO6sM+XvfiysPkLpQDRfRvm5Fce6TSrKOnYNqubSNc=; b=HnAhX5T9DHn2AetAynjXwUeQyoAVoefGCIEIUKsghuwufX3E6U+iuirgoFEhdiqyh3 9vjBrhFpwU795oZzHrY7GrdtWhaflf70YF8Ohzol6Hbkvtq0oqkYglO/WJgYD4fFKyPN w13uaOv52B7y7FPiAaFY9J5VrUzUEvl5wqYd2aQ966sIq8T/QNFjBR0XZKm0U7X8f5iX WK3GMss5tkvN954bW9HJLZmhrYWSxfEAEloLcZDP+jCLO4HstqQ2rLn2gc/36IaLEQf+ kC+kAiRLf+gmbNhuYWE8XaBBpGT/BRuHjFXlcnogqx7wyv+5kFDlcpzQv+JKov1QBf58 yEmg== 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=SiO6sM+XvfiysPkLpQDRfRvm5Fce6TSrKOnYNqubSNc=; b=MOkx/lcCRnH+/74y3Q+vUv0s7D6xp1TZnWFrMjXEZv2ALg7U443+vNcS+v4RNWcvG6 D71dQKOrb7jf1BSXW1HCrNM8qsWL2R8VVoAxXzGW0QdiLrYKgIw3VVRKnEV32U2QlbZK kO/KBs8aR/4g65ENjx8qXWff4YlZrPxn2lqy2TlxSan9x7Hz6aQs7U1yGkpqUfXea8wD J6JIz6OHunJ8apnWNhhR7zeY8PN+UaXjpkTeIHsA5Sb2v5s/z+oBwcLwOn7t33PLnsiR bOkUdLzOR3cQpVkCKT+eFJnrMXntXC78rLeASfv1rZRGXXK8lZcXeao42PpyRDIjZIrD /hbg== X-Gm-Message-State: AOAM53283bqxJez9FYSlA/2Dysd9u/WA3cj5cTtWck7x/lCb1Yp7is5q +Ux8yYj2OPgU/WOsUXKM1wA= X-Google-Smtp-Source: ABdhPJxbuImpLy8fRkcJqnFJWjmponTKI4guhH90PnxD4QIJh625euNgLNhLjxpPYSYiWL5uEwfVXw== X-Received: by 2002:a05:6638:e8f:: with SMTP id p15mr1632084jas.69.1625184708815; Thu, 01 Jul 2021 17:11:48 -0700 (PDT) Received: from john-XPS-13-9370.lan ([172.243.157.240]) by smtp.gmail.com with ESMTPSA id c23sm704010ioz.42.2021.07.01.17.11.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jul 2021 17:11:48 -0700 (PDT) From: John Fastabend To: ast@kernel.org, daniel@iogearbox.net, andriin@fb.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, john.fastabend@gmail.com Subject: [PATCH v2 bpf 1/2] bpf, sockmap: fix potential msg memory leak Date: Thu, 1 Jul 2021 17:11:22 -0700 Message-Id: <20210702001123.728035-2-john.fastabend@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210702001123.728035-1-john.fastabend@gmail.com> References: <20210702001123.728035-1-john.fastabend@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 --- net/core/skmsg.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 9b6160a191f8..22603289c2b2 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -505,8 +505,10 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb, * drop the skb. We need to linearize the skb so that the mapping * in skb_to_sgvec can not error. */ - if (skb_linearize(skb)) + if (skb_linearize(skb)) { + kfree(msg); return -EAGAIN; + } num_sge = skb_to_sgvec(skb, msg->sg.data, 0, skb->len); if (unlikely(num_sge < 0)) { kfree(msg); From patchwork Fri Jul 2 00:11:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 469410 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 D121BC11F6A for ; Fri, 2 Jul 2021 00:11:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B52236135C for ; Fri, 2 Jul 2021 00:11:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234457AbhGBAOa (ORCPT ); Thu, 1 Jul 2021 20:14:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234195AbhGBAO3 (ORCPT ); Thu, 1 Jul 2021 20:14:29 -0400 Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD802C061762; Thu, 1 Jul 2021 17:11:57 -0700 (PDT) Received: by mail-il1-x12e.google.com with SMTP id g3so8178248ilj.7; Thu, 01 Jul 2021 17:11:57 -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=/u4U2tQ+aacHBevuu9MBKUgp3LrIgZs5N/Tklyr9zUo=; b=Gc+mfUcIQwE1kpduy/f3CcyfAgaEZRDlyv1iuPxNgDcAjYytjj0lexW9Mbyy5JkHxE J9r4PY9P74jJsOVTxPw+GUYpTH2xAHFmpaq4fgEitYHtMxsIna8uRULtAZD+HWA1WXGu MOg17Rv+Lg8NQmujy2KQdTxwTrsGsfLOvoyGNHHBKCicpYO/p6EpYVYpUslp9N/2TOfA 67xf/dEL6ZvNegPsiKWyAodVzsb4UmlEqtZfF7JZrr48JxRgL3B8VpuzXtf6XVBcIeu3 9icIlRuPoZ4sYTv7tFtKqgkbeHONarna52YAmRiwJVIpgKSMcvbuYHmTOnmSA9uuwO4X Gk2Q== 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=/u4U2tQ+aacHBevuu9MBKUgp3LrIgZs5N/Tklyr9zUo=; b=RS4PV5heETrKLppYTqN9eIjbWopDjfsD/ZStXD21nuBxfcMzoauyQBoCagCUM2k/Xt abnHF8e7yi8E+nN9xZAvVDO1LJQAHtBwDBg3cjCFYBcY+i2zu+gaJRb4BW91YuR9dGa9 FhxWk97FvXFzmTeXykwJ6c7WC/NhOLrjA6sHeBGDVG9zP3i8O0zP/SisNv+eumkWvzHT FpVw1KOKO6zqgwR6TZJ6xvWj9lSGI0sSRDNlR4eo8ea/q7pPA4JxhWM4kYGKTTSEFA/n USdOVawCCdveewhhXB2CThiuHKy1T8aWaRyGw3Em/dRhTd25e4a2h7eZNPridqga+V69 POBw== X-Gm-Message-State: AOAM530n2oJxFIs1KH4xRe5zAR7ZOE3zCgd1TxnBjhPm+uNNZZYNlPDG yr2OnjZ71ryoahsYEGSe+ZQ= X-Google-Smtp-Source: ABdhPJyALz1BOitl2Yy7Ktv+QBDy4J1g6+vUha2fX02aXy5zCdvh4m72/nU/WZSDc3SvN2sf0pFFUw== X-Received: by 2002:a92:3610:: with SMTP id d16mr739552ila.16.1625184717172; Thu, 01 Jul 2021 17:11:57 -0700 (PDT) Received: from john-XPS-13-9370.lan ([172.243.157.240]) by smtp.gmail.com with ESMTPSA id c23sm704010ioz.42.2021.07.01.17.11.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jul 2021 17:11:56 -0700 (PDT) From: John Fastabend To: ast@kernel.org, daniel@iogearbox.net, andriin@fb.com Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, john.fastabend@gmail.com Subject: [PATCH v2 bpf 2/2] bpf, sockmap: sk_prot needs inuse_idx for proc stats Date: Thu, 1 Jul 2021 17:11:23 -0700 Message-Id: <20210702001123.728035-3-john.fastabend@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210702001123.728035-1-john.fastabend@gmail.com> References: <20210702001123.728035-1-john.fastabend@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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. Fixes: 604326b41a6fb ("bpf, sockmap: convert to generic sk_msg interface") Signed-off-by: John Fastabend --- net/core/sock_map.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/net/core/sock_map.c b/net/core/sock_map.c index 60decd6420ca..016ea5460f8f 100644 --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -222,6 +222,9 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk) struct bpf_prog *msg_parser = NULL; struct sk_psock *psock; int ret; +#ifdef CONFIG_PROC_FS + int idx; +#endif /* Only sockets we can redirect into/from in BPF need to hold * refs to parser/verdict progs and have their sk_data_ready @@ -293,9 +296,15 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk) if (msg_parser) psock_set_prog(&psock->progs.msg_parser, msg_parser); +#ifdef CONFIG_PROC_FS + idx = sk->sk_prot->inuse_idx; +#endif ret = sock_map_init_proto(sk, psock); if (ret < 0) goto out_drop; +#ifdef CONFIG_PROC_FS + sk->sk_prot->inuse_idx = idx; +#endif write_lock_bh(&sk->sk_callback_lock); if (stream_parser && stream_verdict && !psock->saved_data_ready) {