From patchwork Fri Oct 6 20:17:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 149393 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp573641vqo; Fri, 6 Oct 2023 13:17:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLjL/oS36fkf0aVqy6TXpx/tcuMTDg7mvjAnaTGzsS6PzxsxCbl7a39pozAuKBkcPDJfGz X-Received: by 2002:a05:6a20:1047:b0:16b:8b82:bcee with SMTP id gt7-20020a056a20104700b0016b8b82bceemr2869072pzc.26.1696623441821; Fri, 06 Oct 2023 13:17:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696623441; cv=none; d=google.com; s=arc-20160816; b=ZZP3rakL/vfOQlhUZ9blH1vdR57peEnEsC8Fg1Mp0A4FAoU8eIonJ8bLZxJ3dLsHle Bf3SlWSeqzI0DnyIhA5ycTysy6BVkBgnZgdHt4FjS7cM+M+o5jke5aqH3A7PPH8CjqI9 OjjMbNbmdWmH1hKI+jSmGk4gBSLmSMMAYKEDJw8HvWEIv2ZatI2+48fG0tcF1Jn/f+4k psNsopDf94BCcT+8MoN3xsA1d1Uu/X4gB4Mvl3AttXnOfh3Ht60H4QhcnH+cR7RB03as 6ZoldD9k8kPUQC0ql/3fDqV+X54IKxsCeM5bj/ezfAfgCvGEdyd2x7VZwJJEtU8QAsgK Ah8Q== 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=SYgd9NJoGeVd+4g2HyXZYAbiV1zNgq5LXMke4LgVM60=; fh=8rn6yqJcx+4XjX7EDUoKDitbo+5/zZ4h3cfnmstOud0=; b=cQs2lwPbZbFsWfelrEPlUpZTbrw76an2JVQ22klOwpu2OcXFnKVjr6gV86ZDZMHIwV CEyFOOSUPp8dymqfRDzhGmqt464l8zcAjDe6y+KFLTDAQylKtOmkULyxZbS3AyuO/vCy ACMTDWfBTEU9ygL4UouMFonjwgvULEaE28ty8cOrB8o3YfJGU9rsSYvCCymmYo9k5BOf BWXUCj5aeK26MKTuAwpSx2dv/7Azo1ryfq1//KXgEPp5pKJoWnq5Lv/f42UDf/EZyr34 w5toJB7OpP8+2DYUcVA0PqU2oQP1MnSn2ptSD0lS8OMhHafbgSIOujGjD/tHNiGTo2Fp IHvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=glIQtzsV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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. [23.128.96.38]) by mx.google.com with ESMTPS id j8-20020a170903024800b001c8944c933dsi1513705plh.380.2023.10.06.13.17.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 13:17:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=glIQtzsV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 A14C8809FA44; Fri, 6 Oct 2023 13:17:19 -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 S233410AbjJFURJ (ORCPT + 18 others); Fri, 6 Oct 2023 16:17:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233167AbjJFURI (ORCPT ); Fri, 6 Oct 2023 16:17:08 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDA74BE for ; Fri, 6 Oct 2023 13:17:03 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-692b2bdfce9so2217946b3a.3 for ; Fri, 06 Oct 2023 13:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1696623423; x=1697228223; 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=SYgd9NJoGeVd+4g2HyXZYAbiV1zNgq5LXMke4LgVM60=; b=glIQtzsVltLJbPBFndOe3pOHcLeCDAEaLQ22HcKgV7jpj++ELRZ/kKVwxirUVEiOTG XzsD49z7sXHoG6Dy3IQLvFSdAgQ7FpKTlvC6aIwAfsIJUrFsOVz7EXZcExrW0GapI4j0 ln04IfZjypBCNGI6nwYayqP8K/oW387tby1h8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696623423; x=1697228223; 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=SYgd9NJoGeVd+4g2HyXZYAbiV1zNgq5LXMke4LgVM60=; b=YRPYzGXdzNLtxmDSKDJe8kSBrY+vWQOUzxy3vMNwn92MPMekljRWWdFKoWZ9LcFRnk f0rBmxVIeCY34oKjMUyiPw2ryA2lpghUsqI+/sSGeQ7isU96bbrTfCTbcasLp6JUC5KD 5U1qhUm/eTe6X61UqL/RB0a5zQGHXla/uWTZyN/MFZb+vq3ovXL57BjigeD/E/t5Bs3H 2a+s5i+p+JwquzBxkr1AuORdfgSyyx1XAgT0bE3cKhUrRnHEp4Sh6paFuYBmGZv3bY86 2ijygZS3l6K5GwcLcjxQ9/URPEAnpBa99EX49BkaRKa2/SLBmGPlzGCAaiDtGeodGEny 5iTQ== X-Gm-Message-State: AOJu0YzGYyO6kFHfEdTqiKgxzLdnYLlmKGJIOYRh6IOzQiMtE7G2Tv6I IeRFEvYM2vnAjCFIgvv5VtkPNw== X-Received: by 2002:a05:6a20:324d:b0:14d:7511:1c2 with SMTP id hm13-20020a056a20324d00b0014d751101c2mr8303161pzc.55.1696623423132; Fri, 06 Oct 2023 13:17:03 -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 w18-20020aa78592000000b0064f76992905sm1879945pfn.202.2023.10.06.13.17.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 13:17:02 -0700 (PDT) From: Kees Cook To: Song Liu Cc: Kees Cook , Jiri Olsa , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , "Gustavo A. R. Silva" , bpf@vger.kernel.org, linux-hardening@vger.kernel.org, Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH] bpf: Annotate struct bpf_stack_map with __counted_by Date: Fri, 6 Oct 2023 13:17:00 -0700 Message-Id: <20231006201657.work.531-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1619; i=keescook@chromium.org; h=from:subject:message-id; bh=Cn0rZ3uKUzlAqWZVBxaI1s9wb/BhJmGzsMuyAyrT7Jg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlIGs8PUE7GHQQH9B2FtvPO9v/D7n9b+rl68H2W RKqjf8h0JKJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZSBrPAAKCRCJcvTf3G3A JtaTEACURo4jMkXwmgS7m4ShxZQ4TwAfA6ZzmjYsh6vYRU9jgLzxlVmFZGarKW2zf9Cv86qoTQB OuLpPhBaNyLhx6zEvR7V9tnxUwEkqNowljuks8/uiTM7dKUMCX8mPrr0W8i7NY3rag314m42553 4Qg7E6CGkIv9FjnVwEy+WwyO4gtQ9T6LSBaDiqznryLw4DMRHSPX5FY1RreXN0hu2KlSN6NE/95 6y+nhBnSpe5AG0WcCaKwduCSLQAYDwu5gSLTQC8ufTlI+8wLVB9RxmJVplQ6ifVtmjwG0mal2Q9 g4CWK58aEl82E6jwl1ibHfcGfj1BOPSrjGWgcVRQGS4Oz95mMtS36TUv3GF/NGMEYbeVMSAOYF/ dZ+ZDcDelDkkrftFCQ0GblqS/FPjlacGmdkA99CUVIKR0c9Dwzz2pJCPVrLvu/vVGqSoU2DKd2O S/u5edTIQKXF+6S74bRXmCTvYDFgRzeDFVbxJ/gKxodnhRJZOwKgzpFeqz2h5A2DjvMkysoTOLr QgNDS9ZfejJ6ptqnkCoFaLOYYEdwTTjLQ/2vQ0ruMLTLZWT6fYMQYrP/Cd9bfrPxcuNg6YA1iqv 5A2La5ev+YTxG4cMliYTclB/18XZ5/C74z+hmZYBC6SCICu7BofIrV+WBsrWOqzu+iMD2dRwPe0 DtdkIXf jUFo8IGQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Spam-Status: No, score=2.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no 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]); Fri, 06 Oct 2023 13:17:19 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779038622128690716 X-GMAIL-MSGID: 1779038622128690716 Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). As found with Coccinelle[1], add __counted_by for struct bpf_stack_map. Cc: Song Liu Cc: Jiri Olsa Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Andrii Nakryiko Cc: Martin KaFai Lau Cc: Yonghong Song Cc: John Fastabend Cc: KP Singh Cc: Stanislav Fomichev Cc: Hao Luo Cc: "Gustavo A. R. Silva" Cc: bpf@vger.kernel.org Cc: linux-hardening@vger.kernel.org Link: https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci [1] Signed-off-by: Kees Cook Reviewed-by: Gustavo A. R. Silva Acked-by: Stanislav Fomichev --- kernel/bpf/stackmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 458bb80b14d5..d6b277482085 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -28,7 +28,7 @@ struct bpf_stack_map { void *elems; struct pcpu_freelist freelist; u32 n_buckets; - struct stack_map_bucket *buckets[]; + struct stack_map_bucket *buckets[] __counted_by(n_buckets); }; static inline bool stack_map_use_build_id(struct bpf_map *map)