All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Antoine Tenart <atenart@kernel.org>,
	Jon Maxwell <jmaxwell37@gmail.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Curtis Taylor <cutaylor-pub@yahoo.com>,
	Martin KaFai Lau <kafai@fb.com>, Sasha Levin <sashal@kernel.org>
Subject: [PATCH 5.4 17/60] bpf: Fix request_sock leak in sk lookup helpers
Date: Mon, 27 Jun 2022 13:21:28 +0200	[thread overview]
Message-ID: <20220627111928.168248168@linuxfoundation.org> (raw)
In-Reply-To: <20220627111927.641837068@linuxfoundation.org>

From: Jon Maxwell <jmaxwell37@gmail.com>

[ Upstream commit 3046a827316c0e55fc563b4fb78c93b9ca5c7c37 ]

A customer reported a request_socket leak in a Calico cloud environment. We
found that a BPF program was doing a socket lookup with takes a refcnt on
the socket and that it was finding the request_socket but returning the parent
LISTEN socket via sk_to_full_sk() without decrementing the child request socket
1st, resulting in request_sock slab object leak. This patch retains the
existing behaviour of returning full socks to the caller but it also decrements
the child request_socket if one is present before doing so to prevent the leak.

Thanks to Curtis Taylor for all the help in diagnosing and testing this. And
thanks to Antoine Tenart for the reproducer and patch input.

v2 of this patch contains, refactor as per Daniel Borkmann's suggestions to
validate RCU flags on the listen socket so that it balances with bpf_sk_release()
and update comments as per Martin KaFai Lau's suggestion. One small change to
Daniels suggestion, put "sk = sk2" under "if (sk2 != sk)" to avoid an extra
instruction.

Fixes: f7355a6c0497 ("bpf: Check sk_fullsock() before returning from bpf_sk_lookup()")
Fixes: edbf8c01de5a ("bpf: add skc_lookup_tcp helper")
Co-developed-by: Antoine Tenart <atenart@kernel.org>
Signed-off-by: Antoine Tenart <atenart@kernel.org>
Signed-off-by: Jon Maxwell <jmaxwell37@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Curtis Taylor <cutaylor-pub@yahoo.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/56d6f898-bde0-bb25-3427-12a330b29fb8@iogearbox.net
Link: https://lore.kernel.org/bpf/20220615011540.813025-1-jmaxwell37@gmail.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 net/core/filter.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index b0df4ddbe30c..eba96343c7af 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -5313,10 +5313,21 @@ __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
 					   ifindex, proto, netns_id, flags);
 
 	if (sk) {
-		sk = sk_to_full_sk(sk);
-		if (!sk_fullsock(sk)) {
+		struct sock *sk2 = sk_to_full_sk(sk);
+
+		/* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
+		 * sock refcnt is decremented to prevent a request_sock leak.
+		 */
+		if (!sk_fullsock(sk2))
+			sk2 = NULL;
+		if (sk2 != sk) {
 			sock_gen_put(sk);
-			return NULL;
+			/* Ensure there is no need to bump sk2 refcnt */
+			if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
+				WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
+				return NULL;
+			}
+			sk = sk2;
 		}
 	}
 
@@ -5350,10 +5361,21 @@ bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
 					 flags);
 
 	if (sk) {
-		sk = sk_to_full_sk(sk);
-		if (!sk_fullsock(sk)) {
+		struct sock *sk2 = sk_to_full_sk(sk);
+
+		/* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
+		 * sock refcnt is decremented to prevent a request_sock leak.
+		 */
+		if (!sk_fullsock(sk2))
+			sk2 = NULL;
+		if (sk2 != sk) {
 			sock_gen_put(sk);
-			return NULL;
+			/* Ensure there is no need to bump sk2 refcnt */
+			if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
+				WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
+				return NULL;
+			}
+			sk = sk2;
 		}
 	}
 
-- 
2.35.1




  parent reply	other threads:[~2022-06-27 11:31 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-27 11:21 [PATCH 5.4 00/60] 5.4.202-rc1 review Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 01/60] vt: drop old FONT ioctls Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 02/60] random: schedule mix_interrupt_randomness() less often Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 03/60] ALSA: hda/via: Fix missing beep setup Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 04/60] ALSA: hda/conexant: " Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 05/60] ALSA: hda/realtek - ALC897 headset MIC no sound Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 06/60] ALSA: hda/realtek: Add quirk for Clevo PD70PNT Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 07/60] net: openvswitch: fix parsing of nw_proto for IPv6 fragments Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 08/60] mmc: sdhci-pci-o2micro: Fix card detect by dealing with debouncing Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 09/60] mtd: rawnand: gpmi: Fix setting busy timeout setting Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 10/60] ata: libata: add qc->flags in ata_qc_complete_template tracepoint Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 11/60] dm era: commit metadata in postsuspend after worker stops Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 12/60] dm mirror log: clear log bits up to BITS_PER_LONG boundary Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 13/60] random: quiet urandom warning ratelimit suppression message Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 14/60] USB: serial: option: add Telit LE910Cx 0x1250 composition Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 15/60] USB: serial: option: add Quectel EM05-G modem Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 16/60] USB: serial: option: add Quectel RM500K module support Greg Kroah-Hartman
2022-06-27 11:21 ` Greg Kroah-Hartman [this message]
2022-06-27 11:21 ` [PATCH 5.4 18/60] phy: aquantia: Fix AN when higher speeds than 1G are not advertised Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 19/60] bonding: ARP monitor spams NETDEV_NOTIFY_PEERS notifiers Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 20/60] net/sched: sch_netem: Fix arithmetic in netem_dump() for 32-bit platforms Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 21/60] drm/msm/mdp4: Fix refcount leak in mdp4_modeset_init_intf Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 22/60] erspan: do not assume transport header is always set Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 23/60] net/tls: fix tls_sk_proto_close executed repeatedly Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 24/60] udmabuf: add back sanity check Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 25/60] x86/xen: Remove undefined behavior in setup_features() Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 26/60] MIPS: Remove repetitive increase irq_err_count Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 27/60] afs: Fix dynamic root getattr Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 28/60] ice: ethtool: advertise 1000M speeds properly Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 29/60] regmap-irq: Fix a bug in regmap_irq_enable() for type_in_mask chips Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 30/60] igb: Make DMA faster when CPU is active on the PCIe link Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 31/60] virtio_net: fix xdp_rxq_info bug after suspend/resume Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 32/60] Revert "net/tls: fix tls_sk_proto_close executed repeatedly" Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 33/60] gpio: winbond: Fix error code in winbond_gpio_get() Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 34/60] s390/cpumf: Handle events cycles and instructions identical Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 35/60] iio: adc: vf610: fix conversion mode sysfs node name Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 36/60] xhci: turn off port power in shutdown Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 37/60] usb: chipidea: udc: check request status before setting device address Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 38/60] iio:chemical:ccs811: rearrange iio trigger get and register Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 39/60] iio:accel:bma180: " Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 40/60] iio:accel:mxc4005: " Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 41/60] iio: accel: mma8452: ignore the return value of reset operation Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 42/60] iio: gyro: mpu3050: Fix the error handling in mpu3050_power_up() Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 43/60] iio: trigger: sysfs: fix use-after-free on remove Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 44/60] iio: adc: stm32: fix maximum clock rate for stm32mp15x Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 45/60] iio: adc: axp288: Override TS pin bias current for some models Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 46/60] xtensa: xtfpga: Fix refcount leak bug in setup Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 47/60] xtensa: Fix refcount leak bug in time.c Greg Kroah-Hartman
2022-06-27 11:21 ` [PATCH 5.4 48/60] parisc: Enable ARCH_HAS_STRICT_MODULE_RWX Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 49/60] powerpc: Enable execve syscall exit tracepoint Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 50/60] powerpc/rtas: Allow ibm,platform-dump RTAS call with null buffer address Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 51/60] powerpc/powernv: wire up rng during setup_arch Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 52/60] ARM: dts: imx6qdl: correct PU regulator ramp delay Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 53/60] ARM: exynos: Fix refcount leak in exynos_map_pmu Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 54/60] soc: bcm: brcmstb: pm: pm-arm: Fix refcount leak in brcmstb_pm_probe Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 55/60] ARM: Fix refcount leak in axxia_boot_secondary Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 56/60] ARM: cns3xxx: Fix refcount leak in cns3xxx_init Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 57/60] modpost: fix section mismatch check for exported init/exit sections Greg Kroah-Hartman
2022-06-27 17:03   ` Florian Fainelli
2022-06-28 19:11     ` Nick Desaulniers
2022-06-28 20:32       ` Florian Fainelli
2022-06-29  5:38       ` Christoph Hellwig
2022-06-29 16:59         ` Nick Desaulniers
2022-06-29 16:59           ` Nick Desaulniers
2022-06-30  9:21           ` Greg Kroah-Hartman
2022-06-30  9:21             ` Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 58/60] random: update comment from copy_to_user() -> copy_to_iter() Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 59/60] kbuild: link vmlinux only once for CONFIG_TRIM_UNUSED_KSYMS (2nd attempt) Greg Kroah-Hartman
2022-06-27 11:22 ` [PATCH 5.4 60/60] powerpc/pseries: wire up rng during setup_arch() Greg Kroah-Hartman
2022-06-27 14:44 ` [PATCH 5.4 00/60] 5.4.202-rc1 review Jon Hunter
2022-06-27 16:27 ` Florian Fainelli
2022-06-27 19:08 ` Daniel Díaz
2022-06-27 21:58 ` Shuah Khan
2022-06-27 23:41 ` Guenter Roeck
2022-06-28 13:16 ` Sudip Mukherjee
2022-06-29  1:08 ` Samuel Zou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220627111928.168248168@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=atenart@kernel.org \
    --cc=cutaylor-pub@yahoo.com \
    --cc=daniel@iogearbox.net \
    --cc=jmaxwell37@gmail.com \
    --cc=kafai@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sashal@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.