From patchwork Mon Oct 16 21:27:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 153762 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp3733720vqb; Mon, 16 Oct 2023 14:27:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHSn7IEcpTZ05cZcuUnm8JhTyuwoO2wTIiICRlgZfTDc7ez5LGqPrX2wQ0ENTd36bf5l7NZ X-Received: by 2002:a17:90b:4c8b:b0:27d:2762:2728 with SMTP id my11-20020a17090b4c8b00b0027d27622728mr418719pjb.0.1697491678773; Mon, 16 Oct 2023 14:27:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697491678; cv=none; d=google.com; s=arc-20160816; b=OgubqYRJnzu0Qxpt9KU/PnYWK5vWdM+q4YTw0qpCNhI+kVJSlT0ohXW9wtCPkxv4sW hEt3LLFar+/IUG4+iOhDRwKyLbIPKU0y48upB0qcCcLKAzh6lgg/pKJ9hiG5Speu0EkJ p/E71j+Zu0fyxKdY+1Crf+U7xOUiISH2crNtpkWTc2vrWEbgc9dcAiBNeGdjQOaPHpX0 XnWeVIYsCtHLNDD/qCeUm28aBqC04IccGhPuE0iQJ8oc1hJ9VPvdEkyrNsuB4j3X0h4y Me7nBwpMK7wdjeod408NLXmeUMbEqp4z6UuV5TACDZFk5aUw3OEIej3q2mkiuNHbS4Xl HCig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=hPgMlBTFx+2I9248zEaT5u1u+rLOVrvQ5zC0LJfwWQ0=; fh=DQSc4j4325ucCh8buQx6ZWNdMkxGyBeIjCjy4nYDRII=; b=UCCqMLaYnYZXefjmESMp1iBA9u+5+qtpazLA4EIXwJ5M+brsuf9ZzxSmwWc4PIGP96 /V9rzHWyyPjM8l9iGBAeBPBSJ51zSJTi2NQfov0Uq80tE5iwguskt7F82F8SUoeiEHjF LlGwhzOqeyqKq+CKu8J+0IgGPfTZEZ+T/Ulh8/yTyQzmaHk7zaM1zm37L7f0bFwWO57C 93yyw17iDgO/Enz4/o0Ud+SnZm8XUhBp6AWB4SDq+B/pl6gs69XcM17rSdhhqrj0S/6Z Hs9q3/2hO9UQmh267gQv8bAMZdc/dtrquijI4LcaCV+2BI0ycyX7sB4/ue4NRqXN/A62 utYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SyF8p1sI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id hg17-20020a17090b301100b002775f7dbd79si122864pjb.184.2023.10.16.14.27.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 14:27:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SyF8p1sI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 435298079334; Mon, 16 Oct 2023 14:27:56 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232577AbjJPV1s (ORCPT + 18 others); Mon, 16 Oct 2023 17:27:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229666AbjJPV1r (ORCPT ); Mon, 16 Oct 2023 17:27:47 -0400 Received: from mail-oo1-xc35.google.com (mail-oo1-xc35.google.com [IPv6:2607:f8b0:4864:20::c35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 911CBA7 for ; Mon, 16 Oct 2023 14:27:44 -0700 (PDT) Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-57b67c84999so3328009eaf.3 for ; Mon, 16 Oct 2023 14:27:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1697491664; x=1698096464; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=hPgMlBTFx+2I9248zEaT5u1u+rLOVrvQ5zC0LJfwWQ0=; b=SyF8p1sIKoQrRUTRh2B/L+4Sp0wytVvB7HernGTi975VcWIE9ml0f93H6nv67nbAKw 9YI0PQPayOpDkaRrxlHfBNQLEeRpu+RfifMWJ0lEbmZJWJJG1qKccfvPhoufh1rIPlVO IOuKXrFwzd5Ny2c28x5IguUtoe0XaZLn0cK6g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697491664; x=1698096464; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hPgMlBTFx+2I9248zEaT5u1u+rLOVrvQ5zC0LJfwWQ0=; b=b0GVsFn8T9cre7MDukSFUzyVQxgA/jfXAg3q/R+ji4Irjore2Y8U/moMRku8kWnbPA hM+EGihNio05LYUoyhYh2Wt3RSERbU9KIcVy6cHqrMl9ebE0QO3r/JC0jSwASqBHlsaq lsGGwVjVvLpC6CHC4F2PevCBQ9aBXXZMwRftPv660wnNQlZRKIYb7ETANtf/RIot5j1u f73zbNah7EDNAM05BkqJytf+9kBX29yKiP7OYcMkriea8nvyCQ3ZNlbDDwSAdPvS3eJg aT2J742nLpgUwX7V4D2h7hLxSlTCPmVRFXkKgdqwWeIgPOk9n3Q1ScSE6HqtwBQBj0XQ QrIg== X-Gm-Message-State: AOJu0YxeJeQunCnVlspFcKFnzVPphAVGy2KZkbpNV1jRih4qEe/frI3x 7HsPn1AgAeWy8Tuj9GchNDJwZZDxl/KCnZpVv2k= X-Received: by 2002:a05:6359:5ea0:b0:166:d379:93c1 with SMTP id px32-20020a0563595ea000b00166d37993c1mr587200rwb.20.1697491663843; Mon, 16 Oct 2023 14:27:43 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id a6-20020aa78e86000000b0068fd026b496sm30364pfr.46.2023.10.16.14.27.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 14:27:43 -0700 (PDT) From: Kees Cook To: Kent Overstreet Cc: Kees Cook , Brian Foster , linux-bcachefs@vger.kernel.org, kernel test robot , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v2] bcachefs: Refactor bkey_i to use a flexible array Date: Mon, 16 Oct 2023 14:27:39 -0700 Message-Id: <20231016212735.it.314-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3237; i=keescook@chromium.org; h=from:subject:message-id; bh=ZFVX4Et7GPEikNCf0Ix6A5YSoE2Mfb24IGbRkCLbcNc=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlLarL3fT0ZlYwcbjnSnTWM77NW/ozenkBQEVl3 aupJJB3LnOJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZS2qywAKCRCJcvTf3G3A Jse1D/9vmoEVewSjtb1/R8Ur1UqM8PbdX33bLxi16QrR0H+U/mxcWnnEzugwd0LqR/HJBaNordc w/uC/7a8P2f9nW9QhXS6j2wo3U59Kexpsd5S60GxyiLtEwJA51JOEM/BHu5AreNoUPVivpINC0M gY+EsSE7uvNa0Wm+ZF22AmwcY2pTf+qUyy6uK6tPsaCevoMDKpFEbgxVp2MkHaUfOCb7hNlj+r/ lDmfxxjxpW2IqKxQnsBbnjL9Bdq0+9Bw3j9K6Mi8s8ElzAZy4v334yhHKxuF7fm+8vU8S+oPIJ1 16XKru1DChGWe8pxxL2Li1YuXsP8F1BkGwBdWpfcWLVeP08Fat82dEWXYfaBx6f2Xrb4kVGPbny LhvjATPEbg2T2q67a9IfEEMt7eCqcYR9uvd+D4zQkby4rYdx1eSawPu6QM286GD/wift69yoAXl OdbGYJIdw8Wk8Z2FBr3OunPOk1yKD8QrVUoV3fdPxBgIDLtqxdNAQTzosswze4be0ozl2oXYf+s Uy/zjenXqlJT/hNQT1IcTU2UvriDo+0V58793WA77lHpT9+Zfps2oL911dWUDMKXAukmVbOLR4P T0Q3FE/nRDAYEQCOTCxskheCOAFJtdykoWuhGgSgzcvqWGTGMR/WpHj0JK2P7T2GRovBLTMo2Ft n0ZmyMs Pc1bRs0A== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 16 Oct 2023 14:27:56 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779414819781348006 X-GMAIL-MSGID: 1779949034726473850 The memcpy() in bch2_bkey_append_ptr() is operating on an embedded fake flexible array. Instead, make it explicit, and convert the memcpy to target the flexible array instead. Fixes the W=1 warning seen for -Wstringop-overflow: In file included from include/linux/string.h:254, from include/linux/bitmap.h:11, from include/linux/cpumask.h:12, from include/linux/smp.h:13, from include/linux/lockdep.h:14, from include/linux/radix-tree.h:14, from include/linux/backing-dev-defs.h:6, from fs/bcachefs/bcachefs.h:182: fs/bcachefs/extents.c: In function 'bch2_bkey_append_ptr': include/linux/fortify-string.h:57:33: warning: writing 8 bytes into a region of size 0 [-Wstringop-overflow=] 57 | #define __underlying_memcpy __builtin_memcpy | ^ include/linux/fortify-string.h:648:9: note: in expansion of macro '__underlying_memcpy' 648 | __underlying_##op(p, q, __fortify_size); \ | ^~~~~~~~~~~~~ include/linux/fortify-string.h:693:26: note: in expansion of macro '__fortify_memcpy_chk' 693 | #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \ | ^~~~~~~~~~~~~~~~~~~~ fs/bcachefs/extents.c:235:17: note: in expansion of macro 'memcpy' 235 | memcpy((void *) &k->v + bkey_val_bytes(&k->k), | ^~~~~~ fs/bcachefs/bcachefs_format.h:287:33: note: destination object 'v' of size 0 287 | struct bch_val v; | ^ Cc: Kent Overstreet Cc: Brian Foster Cc: linux-bcachefs@vger.kernel.org Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202309192314.VBsjiIm5-lkp@intel.com/ Signed-off-by: Kees Cook Reviewed-by: Gustavo A. R. Silva Reviewed-by: Brian Foster --- v2 - Change flex array name to "v_bytes" (bfoster) v1 - https://lore.kernel.org/r/20231010235609.work.594-kees@kernel.org --- fs/bcachefs/bcachefs_format.h | 5 ++++- fs/bcachefs/extents.h | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/fs/bcachefs/bcachefs_format.h b/fs/bcachefs/bcachefs_format.h index f0d130440baa..cb1af3799b59 100644 --- a/fs/bcachefs/bcachefs_format.h +++ b/fs/bcachefs/bcachefs_format.h @@ -300,7 +300,10 @@ struct bkey_i { __u64 _data[0]; struct bkey k; - struct bch_val v; + union { + struct bch_val v; + DECLARE_FLEX_ARRAY(__u8, v_bytes); + }; }; #define KEY(_inode, _offset, _size) \ diff --git a/fs/bcachefs/extents.h b/fs/bcachefs/extents.h index 7ee8d031bb6c..896fcfca4f21 100644 --- a/fs/bcachefs/extents.h +++ b/fs/bcachefs/extents.h @@ -642,7 +642,7 @@ static inline void bch2_bkey_append_ptr(struct bkey_i *k, struct bch_extent_ptr ptr.type = 1 << BCH_EXTENT_ENTRY_ptr; - memcpy((void *) &k->v + bkey_val_bytes(&k->k), + memcpy(&k->v_bytes[bkey_val_bytes(&k->k)], &ptr, sizeof(ptr)); k->k.u64s++;