From patchwork Fri Sep 22 17:49:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 143737 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:81b:b0:402:e146:ef86 with SMTP id fz27csp1822291vqb; Fri, 22 Sep 2023 17:12:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGaZjby042i9jC5Gw+Abooqu0kPnvWBkwOYcT30gneNCG+W6SKFxEaXVHvxW/UOPQ9yRddx X-Received: by 2002:a17:902:c38d:b0:1bc:9794:22ef with SMTP id g13-20020a170902c38d00b001bc979422efmr995082plg.1.1695427921314; Fri, 22 Sep 2023 17:12:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695427921; cv=none; d=google.com; s=arc-20160816; b=xJkp/4oIxFhvntXIF+0HdExS/77fLpnbaE2Vf6LLMZuR7SRatLMOo2ItfFZOwsFpTG 6AKpOneLueKKBvwel/M6/ei0Qu9tZXzhsOLqM/P+77m/HIpaf2T9sQKriQpE/lCBGF6Z drhy7WeH5eEAaF5OdEbBhqcxRWPD6pUTjIFZKCNlw3evu9FPKlh57Sh+PKIgnJdqquZf VnNv71wApUuMPvr4TzfM1gl+llQAJL/ZVIKe12lcl6yqFWnorAV693NX0MsTyqyIFwN/ pnvUw5j/gJW9taVXch0/8Zks9zQfGc5QCM0KTa122svCBaum2Ki+48Mqb7QyucZq18Uq +GJA== 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=i346clq8OfBFzjIMANH+n2WNr6xQCzrAEDi+whp12qc=; fh=rCkVgMNWqm2YvCnDLKMFgw5xH6ybwABbKpklr5XJotI=; b=xzbLYZTSuHtGryX/o5D5jDEYS6Fu2SiyUsl6k4gUVzR2drD3ssYOsCcD79/7K0mi4V LSV5BLw+VO8AxcD7T8GH2QTT6hsRdb32BKGLUh0t30AA5XVHI65cc3hVptp2cmyJfBB+ 50Fxb9IfRY1S41gMYE4jcOZHVhLV+uG9PNN38d03HBmx3kF9uH0QI62KVpYFUey2GH3U shZvw6NYQj/dgLaaUWYGVSHwdtphYNuUPC1f/yC5Xd/QcsNuEDFR1r99YMV5iXgH5OA6 NSEZQtzrYBx7QhxTk2l78F3DkxsiL19gZZgc2KEdQbgujMxL626tiEQG+WQmaktWd1kR ee9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Lpf995zI; 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 u13-20020a170902e5cd00b001b8ac52a19esi5570543plf.436.2023.09.22.17.12.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 17:12:01 -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=Lpf995zI; 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 3F53183A407A; Fri, 22 Sep 2023 10:50:55 -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 S233142AbjIVRuJ (ORCPT + 28 others); Fri, 22 Sep 2023 13:50:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233426AbjIVRuB (ORCPT ); Fri, 22 Sep 2023 13:50:01 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F00051BE for ; Fri, 22 Sep 2023 10:49:54 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-690bd8f89baso2249203b3a.2 for ; Fri, 22 Sep 2023 10:49:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1695404994; x=1696009794; 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=i346clq8OfBFzjIMANH+n2WNr6xQCzrAEDi+whp12qc=; b=Lpf995zItFq6sR0tGyAyBCBbqloaVSUoihECQF8R6YAWb1piw+dr8wUNLTVEVoz46j Al074oqsdK/IUrEnJwMeL/j1i/8DIGuAjl8XCOtH7tHzG7dtP1/0/kmsDdOoEq6mglEi Ya+zm7x0eydL5BHLdzZ07ZgWJTWFJnKZGihsQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695404994; x=1696009794; 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=i346clq8OfBFzjIMANH+n2WNr6xQCzrAEDi+whp12qc=; b=UpEnsxE5iE6UMc12eKgeDWX0FPPhtEfxnw4wVORgRhOLby+nym1dt/s4uAtwYmpHOf 6TrMlxMDdLTuFn0V4OCq+KixBwFQbvPaqKawCxbUU2xOZlU8Wfu9CJfbmBD0zBGRO1dO 1hAbUBg/YRA55yZl/ffZxAc5GRW9hpXP+tNBs7RuDgtqqnWWC14yS3d4QXl+0HqF7Ntg WIYRuEfHYpQ8auaXQ+naBqhmIM9/ed9gBIx8hjIMvRGh8z3HQ1TWb8vVHNJ7ZpXBOHOU Fn9yxJdcsQaE4iB7Yb6huqeqABlJOxlGW26qNRCptg6Evpy+nrAaRWtgXJNEFXDuV3h5 /tzg== X-Gm-Message-State: AOJu0YwozNJ4kEf5Jd/iHfMya85QKLTsywKn2neJUQ6INecMs4kCkHKy ZzNbeDWZHWItSVNZe7dGu8Xm9w== X-Received: by 2002:a05:6a00:2e93:b0:690:2ad9:1436 with SMTP id fd19-20020a056a002e9300b006902ad91436mr184891pfb.7.1695404994406; Fri, 22 Sep 2023 10:49:54 -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 v13-20020a63b64d000000b0057783b0f102sm3053318pgt.40.2023.09.22.10.49.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 10:49:53 -0700 (PDT) From: Kees Cook To: Mauro Carvalho Chehab Cc: Kees Cook , Hans Verkuil , lijian , linux-media@vger.kernel.org, Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org Subject: [PATCH] media: v4l2-event: Annotate struct v4l2_subscribed_event with __counted_by Date: Fri, 22 Sep 2023 10:49:52 -0700 Message-Id: <20230922174951.work.160-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2092; i=keescook@chromium.org; h=from:subject:message-id; bh=C66/icBEq2MG+m/4ZluAAHZC3P5Kpbkm+mwqC112tHk=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlDdPAkSq5hJos3sXunT3ysggynSqRlnNPBwz8S uw47BLY+beJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZQ3TwAAKCRCJcvTf3G3A JrwJD/9Ga6dhlY0NaypMA6bx76My1z2ArqeY+4D5HEL3TANx74Wr4Kc0p/+suRYVJWhO2AcnxeU 2hrfzl3pi0Vvw9c5dSxjhNykeazY/+6bWYyMMPEZs+nkNWTNsSaNGWSuQlwi/DWkvux/s2CAfma aNW56Oa/XhHaEk7LI2ZSJilEjfkWfZ7Yak8Ud4M2+BCIntq+ML2oE8ms1fCZqoYjtvZ+/UsHTmS g3SYTUAf7cjUXhU15YO3y8zQyNYyZsatTU3HGJNpJAJjL/QYtBELDxXf70bmNHzha8SDMSv1YS8 MgDAkfMLfM7r+R4anEuiL/mXkrtSd6LB6jsKoXgK+VLjG1JP+UUKDIuvcmO7TM1l6VuHWj9WRld yTTwN4mMvPSm47Xx3PUaC6LuTijjcqhA3Ua6nFbW0zvaPg7IpV44SycHiEpioj88TrElu81AFrL kIGefhO0Hpe32pz7zFpcstbvnlZgAvxX88/pxkNiwIBzunAJ48TCSevzWX1fGf3Zt4iDpLr+bT5 V4BjtZntEnOCExVsQWu5ZfbI41epwpEP3p6WJ6KEcVIEcAzKw4q3Hbb0xAxEj0AR75RIOyyofOf 5wYS6DC88fkx+lu3Yu29SH5YEGxqz8hysn6mgDV1W4BcnwHl9dkjhIvY13uFHiexjRTItbx3DEf DL1oEuc RlCqUUwQ== 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,URIBL_BLOCKED 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]); Fri, 22 Sep 2023 10:50:55 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777785028058908567 X-GMAIL-MSGID: 1777785028058908567 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 checking 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 v4l2_subscribed_event. Additionally, since the element count member must be set before accessing the annotated flexible array member, move its initialization earlier. [1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci Cc: Mauro Carvalho Chehab Cc: Hans Verkuil Cc: lijian Cc: linux-media@vger.kernel.org Signed-off-by: Kees Cook Reviewed-by: Gustavo A. R. Silva --- drivers/media/v4l2-core/v4l2-event.c | 2 +- include/media/v4l2-event.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core/v4l2-event.c index c5ce9f11ad7b..3898ff7edddb 100644 --- a/drivers/media/v4l2-core/v4l2-event.c +++ b/drivers/media/v4l2-core/v4l2-event.c @@ -238,6 +238,7 @@ int v4l2_event_subscribe(struct v4l2_fh *fh, sev = kvzalloc(struct_size(sev, events, elems), GFP_KERNEL); if (!sev) return -ENOMEM; + sev->elems = elems; for (i = 0; i < elems; i++) sev->events[i].sev = sev; sev->type = sub->type; @@ -245,7 +246,6 @@ int v4l2_event_subscribe(struct v4l2_fh *fh, sev->flags = sub->flags; sev->fh = fh; sev->ops = ops; - sev->elems = elems; mutex_lock(&fh->subscribe_lock); diff --git a/include/media/v4l2-event.h b/include/media/v4l2-event.h index 4ffa914ade3a..3a0e2588361c 100644 --- a/include/media/v4l2-event.h +++ b/include/media/v4l2-event.h @@ -78,7 +78,7 @@ struct v4l2_subscribed_event { unsigned int elems; unsigned int first; unsigned int in_use; - struct v4l2_kevent events[]; + struct v4l2_kevent events[] __counted_by(elems); }; /**