Message ID | 20240212111737.917428-1-arnd@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-61407-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp2371661dyd; Mon, 12 Feb 2024 03:40:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUCkCLu/dcN641HHhzhhpffVLeWFSyI5l24PSzFhkvuFDWv6i/oJF7MCyRXJZVzKR9oybAcJxXICQAutRm2Y5Tr+1AWLQ== X-Google-Smtp-Source: AGHT+IECendm1LiKfYm+xbm41BYFEL+tX27olhSjxIwBe3SI/SZlCszabqSillaT5frtqW/us2Uf X-Received: by 2002:a05:6808:2e94:b0:3c0:3189:d9e3 with SMTP id gt20-20020a0568082e9400b003c03189d9e3mr6440679oib.15.1707738017275; Mon, 12 Feb 2024 03:40:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707738017; cv=pass; d=google.com; s=arc-20160816; b=d6MvXo+P13cmIsQoFtGkxwaLwqmYSLr6KEKubUQ0SxX6E7b/E9j3e02grozUFKi+TZ HqKwi834I7Q6N0ttmP7vBhxiw1F7tuDbo9Z8QbzlUs6ATSOm9MsTH9WdPIkU7hsBSBJK 1zGBoRy850iPnLvUYPUJWuVf6Y0NlCVhfTn6AbPSAfqkPdwni7d8tzmP1V5BeQnTCxJt CN5k0jst1THlYJoD6iRIEu9ApyTrH34lyYDqa49p+FKFsi2hAjb2DkVvmUgjTPCRn2w0 iHwVMUfv61F4rZsP/6mT5wDyAmEpoMyyXMgrrscbtCq/FurYnMQTkT/sdGXXsphQ0ACB SV+Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=iL+QtMsZTKrSGTb1pzvx7jDYORIO80vzBrYhsMugk+I=; fh=Do31S0SqhDh1eprfjfOU2dbiXyw0cHUgKTXN+O3j+Y8=; b=rwrxSGRZG/2DJhngYJ7NPpsuns3qEYqhJ1cUHaef4MeWQ3Pi8hkEYZZbTUUKi0JQVm utw3bDRgfCLCv3nGA9T+Rd644maVQdA60gApcE24cxXqVkubArsD9LWZJ8q5r/DER91D NyPxwkjzI0ErN0PmDiXFvTMTZHozjkBdnKMr7zNbqMwchlrnZCu2MFaDdwDi0ckr61iS EL06M96CS6z7+vPnvy/TI7Ngu4jmTN6739MulfXF0u//xSSH2D79Qwi17HDSMAMZ++g7 RFLnm8Gz2TJptnrGynxg67ZximtAgt+Uu6BGqO7uUvZ7oYPES06xuspcihoKDDPCth0K ZLjw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uI3t9Err; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-61407-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61407-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCW/oN4nwhIy28kZccbY4cbQ2KMLEkyWUkUbBFBNqO6Zbr7dDj377HWEDwztx09FLUI43rbubV9aPnKEsxQKynaWu2vJlA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id ko19-20020a056a00461300b006e05cb7fd6csi4887294pfb.243.2024.02.12.03.40.16 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 03:40:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61407-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uI3t9Err; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-61407-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61407-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id A308DB24E22 for <ouuuleilei@gmail.com>; Mon, 12 Feb 2024 11:18:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0CA5039AFC; Mon, 12 Feb 2024 11:17:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="uI3t9Err" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FB3C39877 for <linux-kernel@vger.kernel.org>; Mon, 12 Feb 2024 11:17:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707736664; cv=none; b=iBjfEKjHP/Et42baVZR1JtJhmnEZrBpoMCdWuxhM3Fl1YI5tgo5WbZ73iwoQ8MJNlvIsS3QPEaNR+NEaaSRg7AOObx7hjD4dNwm6XjYah/flusqHpz81lw6p9iGPbJEaJXV+sMCSfHi7d56arcsFoQU6eTQMQj+HiA1iaeVE/0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707736664; c=relaxed/simple; bh=YCI5M76ZWV3agMAEsxysptr2SwbMtl3GoSgyNq4kuHM=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=WilFmZqu6qcnSJPweSEY6heB3RguFYEyNYsmJUF4hZdOgCIlm9Sap7Kh/ZvS3UDQA/XviWbtNKlEPty2CRG+7ggS7YszQ46iJkoBcbV27DTHE9j+86be7/6G4DfCDbdtNcjRGIDYOMtn82GyJFzXCwK2hOlGWkew16z+CuQ9F6g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uI3t9Err; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 33642C433C7; Mon, 12 Feb 2024 11:17:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707736664; bh=YCI5M76ZWV3agMAEsxysptr2SwbMtl3GoSgyNq4kuHM=; h=From:To:Cc:Subject:Date:From; b=uI3t9Err5RSXW5W3b0MsjFi/rTEbrQsdY0RZkCxI3OuIZHaoktNEWhszlyUdDfmSG aDryVTPOn9cBxqiUJNl5KopjoaOFBskt+vCtpzF85hlHlRDc45H8rP+DEsSXhT8ATh Ar2dYy++CV/rWqE2HXS3rgwb97LoGCL4aycz1fBRLlNx84BDdBZaccZXg08avAZeIW z/Z600Qbyso+3x1VEngdXtRWxbRcaVRfj5Ny9m2yqtUdeUG0CiYAzXORKYsx87xXVB wwZJZN1oCjBQTmHCK+AKPdpIFZK83ffIipbBza2Sv/1cf4Z8p24yFOqM5nXJ1EO6eI 04D8nrJm8Uygw== From: Arnd Bergmann <arnd@kernel.org> To: Tycho Andersen <tycho@tycho.pizza>, Kees Cook <keescook@chromium.org> Cc: Arnd Bergmann <arnd@arndb.de>, linux-kernel@vger.kernel.org Subject: [PATCH] samples: user-trap: fix strict-aliasing warning Date: Mon, 12 Feb 2024 12:17:31 +0100 Message-Id: <20240212111737.917428-1-arnd@kernel.org> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790693099140097566 X-GMAIL-MSGID: 1790693099140097566 |
Series |
samples: user-trap: fix strict-aliasing warning
|
|
Commit Message
Arnd Bergmann
Feb. 12, 2024, 11:17 a.m. UTC
From: Arnd Bergmann <arnd@arndb.de> I started getting warnings for this one file, though I can't see what changed since it was originally introduced in commit fec7b6690541 ("samples: add an example of seccomp user trap"). samples/seccomp/user-trap.c: In function 'send_fd': samples/seccomp/user-trap.c:50:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] 50 | *((int *)CMSG_DATA(cmsg)) = fd; | ~^~~~~~~~~~~~~~~~~~~~~~~ samples/seccomp/user-trap.c: In function 'recv_fd': samples/seccomp/user-trap.c:83:18: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] 83 | return *((int *)CMSG_DATA(cmsg)); | ~^~~~~~~~~~~~~~~~~~~~~~~ Using a temporary pointer variable avoids the warning. Signed-off-by: Arnd Bergmann <arnd@arndb.de> --- samples/seccomp/user-trap.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
Comments
On Mon, 12 Feb 2024 12:17:31 +0100, Arnd Bergmann wrote: > I started getting warnings for this one file, though I can't see what changed > since it was originally introduced in commit fec7b6690541 ("samples: add an > example of seccomp user trap"). > > samples/seccomp/user-trap.c: In function 'send_fd': > samples/seccomp/user-trap.c:50:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] > 50 | *((int *)CMSG_DATA(cmsg)) = fd; > | ~^~~~~~~~~~~~~~~~~~~~~~~ > samples/seccomp/user-trap.c: In function 'recv_fd': > samples/seccomp/user-trap.c:83:18: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] > 83 | return *((int *)CMSG_DATA(cmsg)); > | ~^~~~~~~~~~~~~~~~~~~~~~~ > > [...] Applied to for-next/seccomp, thanks! [1/1] samples: user-trap: fix strict-aliasing warning https://git.kernel.org/kees/c/9ad28ca5238d Take care,
On Mon, Feb 12, 2024 at 10:42:27AM -0800, Kees Cook wrote: > On Mon, 12 Feb 2024 12:17:31 +0100, Arnd Bergmann wrote: > > I started getting warnings for this one file, though I can't see what changed > > since it was originally introduced in commit fec7b6690541 ("samples: add an > > example of seccomp user trap"). > > > > samples/seccomp/user-trap.c: In function 'send_fd': > > samples/seccomp/user-trap.c:50:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] > > 50 | *((int *)CMSG_DATA(cmsg)) = fd; > > | ~^~~~~~~~~~~~~~~~~~~~~~~ > > samples/seccomp/user-trap.c: In function 'recv_fd': > > samples/seccomp/user-trap.c:83:18: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] > > 83 | return *((int *)CMSG_DATA(cmsg)); > > | ~^~~~~~~~~~~~~~~~~~~~~~~ > > > > [...] > > Applied to for-next/seccomp, thanks! > > [1/1] samples: user-trap: fix strict-aliasing warning > https://git.kernel.org/kees/c/9ad28ca5238d > > Take care, If you happen to update the trailers, looks good to me too: Acked-by: Tycho Andersen <tandersen@netflix.com> I also don't understand what changed, or why this really fixes it. We're still "violating" strict aliasing as far as I can tell, since we just introduce `int *fd_ptr` insted of memcpy()-ing out the fd into an int? But whatever shuts the compiler up works for me. Tycho
diff --git a/samples/seccomp/user-trap.c b/samples/seccomp/user-trap.c index 20291ec6489f..a23fec357b5d 100644 --- a/samples/seccomp/user-trap.c +++ b/samples/seccomp/user-trap.c @@ -33,6 +33,7 @@ static int send_fd(int sock, int fd) { struct msghdr msg = {}; struct cmsghdr *cmsg; + int *fd_ptr; char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c'; struct iovec io = { .iov_base = &c, @@ -47,7 +48,8 @@ static int send_fd(int sock, int fd) cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_len = CMSG_LEN(sizeof(int)); - *((int *)CMSG_DATA(cmsg)) = fd; + fd_ptr = (int *)CMSG_DATA(cmsg); + *fd_ptr = fd; msg.msg_controllen = cmsg->cmsg_len; if (sendmsg(sock, &msg, 0) < 0) { @@ -62,6 +64,7 @@ static int recv_fd(int sock) { struct msghdr msg = {}; struct cmsghdr *cmsg; + int *fd_ptr; char buf[CMSG_SPACE(sizeof(int))] = {0}, c = 'c'; struct iovec io = { .iov_base = &c, @@ -79,8 +82,9 @@ static int recv_fd(int sock) } cmsg = CMSG_FIRSTHDR(&msg); + fd_ptr = (int *)CMSG_DATA(cmsg); - return *((int *)CMSG_DATA(cmsg)); + return *fd_ptr; } static int user_trap_syscall(int nr, unsigned int flags)