public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Kees Cook <keescook@chromium.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Jason Gunthorpe <jgg@nvidia.com>, Sasha Levin <sashal@kernel.org>,
	ndesaulniers@google.com, markzhang@nvidia.com,
	phaddad@nvidia.com, Jason@zx2c4.com, lengchao@huawei.com,
	michaelgur@nvidia.com, linux-rdma@vger.kernel.org,
	llvm@lists.linux.dev
Subject: [PATCH AUTOSEL 6.2 61/64] RDMA/cma: Distinguish between sockaddr_in and sockaddr_in6 by size
Date: Fri,  3 Mar 2023 16:41:03 -0500	[thread overview]
Message-ID: <20230303214106.1446460-61-sashal@kernel.org> (raw)
In-Reply-To: <20230303214106.1446460-1-sashal@kernel.org>

From: Kees Cook <keescook@chromium.org>

[ Upstream commit 876e480da2f74715fc70e37723e77ca16a631e35 ]

Clang can do some aggressive inlining, which provides it with greater
visibility into the sizes of various objects that are passed into
helpers. Specifically, compare_netdev_and_ip() can see through the type
given to the "sa" argument, which means it can generate code for "struct
sockaddr_in" that would have been passed to ipv6_addr_cmp() (that expects
to operate on the larger "struct sockaddr_in6"), which would result in a
compile-time buffer overflow condition detected by memcmp(). Logically,
this state isn't reachable due to the sa_family assignment two callers
above and the check in compare_netdev_and_ip(). Instead, provide a
compile-time check on sizes so the size-mismatched code will be elided
when inlining. Avoids the following warning from Clang:

../include/linux/fortify-string.h:652:4: error: call to '__read_overflow' declared with 'error' attribute: detected read beyond size of object (1st parameter)
                        __read_overflow();
                        ^
note: In function 'cma_netevent_callback'
note:   which inlined function 'node_from_ndev_ip'
1 error generated.

When the underlying object size is not known (e.g. with GCC and older
Clang), the result of __builtin_object_size() is SIZE_MAX, which will also
compile away, leaving the code as it was originally.

Link: https://lore.kernel.org/r/20230208232549.never.139-kees@kernel.org
Link: https://github.com/ClangBuiltLinux/linux/issues/1687
Signed-off-by: Kees Cook <keescook@chromium.org>
Tested-by: Nathan Chancellor <nathan@kernel.org> # build
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/infiniband/core/cma.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c
index 68721ff10255e..7e508b15e7761 100644
--- a/drivers/infiniband/core/cma.c
+++ b/drivers/infiniband/core/cma.c
@@ -479,13 +479,20 @@ static int compare_netdev_and_ip(int ifindex_a, struct sockaddr *sa,
 	if (sa->sa_family != sb->sa_family)
 		return sa->sa_family - sb->sa_family;
 
-	if (sa->sa_family == AF_INET)
-		return memcmp((char *)&((struct sockaddr_in *)sa)->sin_addr,
-			      (char *)&((struct sockaddr_in *)sb)->sin_addr,
+	if (sa->sa_family == AF_INET &&
+	    __builtin_object_size(sa, 0) >= sizeof(struct sockaddr_in)) {
+		return memcmp(&((struct sockaddr_in *)sa)->sin_addr,
+			      &((struct sockaddr_in *)sb)->sin_addr,
 			      sizeof(((struct sockaddr_in *)sa)->sin_addr));
+	}
+
+	if (sa->sa_family == AF_INET6 &&
+	    __builtin_object_size(sa, 0) >= sizeof(struct sockaddr_in6)) {
+		return ipv6_addr_cmp(&((struct sockaddr_in6 *)sa)->sin6_addr,
+				     &((struct sockaddr_in6 *)sb)->sin6_addr);
+	}
 
-	return ipv6_addr_cmp(&((struct sockaddr_in6 *)sa)->sin6_addr,
-			     &((struct sockaddr_in6 *)sb)->sin6_addr);
+	return -1;
 }
 
 static int cma_add_id_to_tree(struct rdma_id_private *node_id_priv)
-- 
2.39.2


  parent reply	other threads:[~2023-03-03 21:44 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-03 21:40 [PATCH AUTOSEL 6.2 01/64] IB/hfi1: Update RMT size calculation Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 02/64] iommu: Remove deferred attach check from __iommu_detach_device() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 03/64] iommu/amd: Fix error handling for pdev_pri_ats_enable() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 04/64] PCI/ACPI: Account for _S0W of the target bridge in acpi_pci_bridge_d3() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 05/64] media: uvcvideo: Remove format descriptions Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 06/64] media: uvcvideo: Handle cameras with invalid descriptors Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 07/64] media: uvcvideo: Handle errors from calls to usb_string Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 08/64] media: uvcvideo: Quirk for autosuspend in Logitech B910 and C910 Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 09/64] media: uvcvideo: Silence memcpy() run-time false positive warnings Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 10/64] USB: fix memory leak with using debugfs_lookup() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 11/64] cacheinfo: Fix shared_cpu_map to handle shared caches at different levels Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 12/64] usb: fotg210: List different variants Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 13/64] dt-bindings: usb: Add device id for Genesys Logic hub controller Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 14/64] staging: emxx_udc: Add checks for dma_alloc_coherent() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 15/64] tty: fix out-of-bounds access in tty_driver_lookup_tty() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 16/64] tty: serial: fsl_lpuart: disable the CTS when send break signal Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 17/64] serial: sc16is7xx: setup GPIO controller later in probe Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 18/64] mei: bus-fixup:upon error print return values of send and receive Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 19/64] parport_pc: Set up mode and ECR masks for Oxford Semiconductor devices Sasha Levin
2023-03-22 23:16   ` Maciej W. Rozycki
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 20/64] tools/iio/iio_utils:fix memory leak Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 21/64] bus: mhi: ep: Fix the debug message for MHI_PKT_TYPE_RESET_CHAN_CMD cmd Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 22/64] iio: accel: mma9551_core: Prevent uninitialized variable in mma9551_read_status_word() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 23/64] iio: accel: mma9551_core: Prevent uninitialized variable in mma9551_read_config_word() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 24/64] media: uvcvideo: Add GUID for BGRA/X 8:8:8:8 Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 25/64] firmware: coreboot: framebuffer: Ignore reserved pixel color bits Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 26/64] soundwire: bus_type: Avoid lockdep assert in sdw_drv_probe() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 27/64] PCI/portdrv: Prevent LS7A Bus Master clearing on shutdown Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 28/64] PCI: loongson: Prevent LS7A MRRS increases Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 29/64] staging: pi433: fix memory leak with using debugfs_lookup() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 30/64] USB: dwc3: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 31/64] USB: chipidea: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 32/64] USB: ULPI: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 33/64] USB: uhci: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 34/64] USB: sl811: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 35/64] USB: fotg210: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 36/64] USB: isp116x: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 37/64] USB: isp1362: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 38/64] USB: gadget: gr_udc: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 39/64] USB: gadget: bcm63xx_udc: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 40/64] USB: gadget: lpc32xx_udc: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 41/64] USB: gadget: pxa25x_udc: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 42/64] USB: gadget: pxa27x_udc: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 43/64] usb: host: xhci: mvebu: Iterate over array indexes instead of using pointer math Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 44/64] USB: ene_usb6250: Allocate enough memory for full object Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 45/64] usb: uvc: Enumerate valid values for color matching Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 46/64] usb: gadget: uvc: Make bSourceID read/write Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 47/64] PCI: Align extra resources for hotplug bridges properly Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 48/64] PCI: Take other bus devices into account when distributing resources Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 49/64] PCI: Distribute available resources for root buses, too Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 50/64] tty: pcn_uart: fix memory leak with using debugfs_lookup() Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 51/64] misc: vmw_balloon: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 52/64] drivers: base: component: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 53/64] drivers: base: dd: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 54/64] kernel/time/test_udelay.c: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 55/64] kernel/power/energy_model.c: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 56/64] kernel/fail_function: " Sasha Levin
2023-03-03 21:40 ` [PATCH AUTOSEL 6.2 57/64] PCI: loongson: Add more devices that need MRRS quirk Sasha Levin
2023-03-03 21:41 ` [PATCH AUTOSEL 6.2 58/64] PCI: Add ACS quirk for Wangxun NICs Sasha Levin
2023-03-03 21:41 ` [PATCH AUTOSEL 6.2 59/64] PCI: pciehp: Add Qualcomm quirk for Command Completed erratum Sasha Levin
2023-03-03 21:41 ` [PATCH AUTOSEL 6.2 60/64] phy: rockchip-typec: Fix unsigned comparison with less than zero Sasha Levin
2023-03-03 21:41 ` Sasha Levin [this message]
2023-03-03 21:41 ` [PATCH AUTOSEL 6.2 62/64] iommu: Attach device group to old domain in error path Sasha Levin
2023-03-03 21:41 ` [PATCH AUTOSEL 6.2 63/64] soundwire: cadence: Remove wasted space in response_buf Sasha Levin
2023-03-03 21:41 ` [PATCH AUTOSEL 6.2 64/64] soundwire: cadence: Drain the RX FIFO after an IO timeout Sasha Levin

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=20230303214106.1446460-61-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=jgg@nvidia.com \
    --cc=keescook@chromium.org \
    --cc=lengchao@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=markzhang@nvidia.com \
    --cc=michaelgur@nvidia.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=phaddad@nvidia.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox