From patchwork Mon Nov 1 06:04:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595843 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0AEB9C4167B for ; Mon, 1 Nov 2021 06:04:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 921846108F for ; Mon, 1 Nov 2021 06:04:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 921846108F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 24E5080010; Mon, 1 Nov 2021 02:04:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D8E180008; Mon, 1 Nov 2021 02:04:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0534D80010; Mon, 1 Nov 2021 02:04:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0164.hostedemail.com [216.40.44.164]) by kanga.kvack.org (Postfix) with ESMTP id E45AB80008 for ; Mon, 1 Nov 2021 02:04:37 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 9D5B1181892E6 for ; Mon, 1 Nov 2021 06:04:37 +0000 (UTC) X-FDA: 78759322236.17.84F24F2 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf18.hostedemail.com (Postfix) with ESMTP id 5404C4002091 for ; Mon, 1 Nov 2021 06:04:37 +0000 (UTC) Received: by mail-pj1-f46.google.com with SMTP id np3so1180941pjb.4 for ; Sun, 31 Oct 2021 23:04:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rZRlybNT1VQOoNxdQue8vo9tAC5dSJmDk+dmEknNUrc=; b=a1M395Bqh+HLw+RAGcbuG3Ee9gQ5Pt14RuYZaJHZtAq8IsesqFhaR4qs895G5hbMKL iQS5vYZGfgH7a5b3h4KU8iCUQfcJ2pMAUZ7xUYJlwNlcv/9R98mCr+O99gLbYNgvnTdf Jq1Q2Y9+75eoLINoepcqswMcLPx+E9zUvTqyDgQoDj1JjQ7GhueGgF6r1EyHzZ4+n1+P 4VyRgUsHzVN0JaYnojDA5tc4RqapLPK5V8yI7l8aREDyn7EoVRQ484iSc2Ox4D6ZVWo3 RXydvoGhJx9s1/xKjIdOKtfqIsQHqS0jolmiHdBqkJBvY53eM6lKKs8AgkFuHiKGjnlr HK4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rZRlybNT1VQOoNxdQue8vo9tAC5dSJmDk+dmEknNUrc=; b=N4mDdWgEym+NvoahZ6Df32QTnmuxuOZAVETIH5qjIFZ2sgFxsFbh+wvOw44tiRGoIE o90NxnistVIgmWLtOT/hOkUjpI7tDHwhywNg2Mv/UQjST9ClPc4zv3IYP+iBhYKjBfuj C4UYtt/3MhvUfKOQgCoHgkcdVYZve0I+6MgYnOKrR3MElnpTMumYAJKq2/nifNnq2R0O KRRh/R3xJAObtE8sgYXYwqqR3ReK1/YNYTaTmWAeEW7V/pC2T68b+1JZioRoBDGj90vG J+Mv5OfzfHDlEK4YeC0b0vgHpNnVXA0k7ZL3BRhK7A7Fezpg/1tWjwyVsLwSMkyOkXC7 kdCQ== X-Gm-Message-State: AOAM5306El3iZ0WNby4m3pT679kUmvJD7+otYkY+bhPlheEjMx7veGjz 3N1N8x04TVtb4xMebDHRgl8= X-Google-Smtp-Source: ABdhPJzL4jPx+ZhAWUkFzp5hvgKpDWtgYKzpi4hD6dRWbzMDiPGRwPGbJb/YNiUpFAuwRHKj4szYdg== X-Received: by 2002:a17:902:8bcb:b0:141:b34e:d54f with SMTP id r11-20020a1709028bcb00b00141b34ed54fmr15789414plo.68.1635746676451; Sun, 31 Oct 2021 23:04:36 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:35 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 03/11] sched.h: use __must_be_array instead of BUILD_BUG_ON in get_task_comm Date: Mon, 1 Nov 2021 06:04:11 +0000 Message-Id: <20211101060419.4682-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=a1M395Bq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Stat-Signature: hkytanee1an5rtbbo3kgqn7a7g6qxrrg X-Rspamd-Queue-Id: 5404C4002091 X-Rspamd-Server: rspam01 X-HE-Tag: 1635746677-618890 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Now that __get_task_comm() will truncate and pad, it's safe to use on both too-small and too-big arrays. So it is not needed to check array length any more. For the original goal of making sure get_task_comm() is being used on a char array, we can use __must_be_array(). Below is the verification when I changed the dest buffer of get_task_comm() in a driver code, CC [M] drivers/infiniband/hw/qib/qib_file_ops.o In file included from ./include/linux/bits.h:22:0, from ./include/linux/ioport.h:13, from ./include/linux/pci.h:31, from drivers/infiniband/hw/qib/qib_file_ops.c:35: drivers/infiniband/hw/qib/qib_file_ops.c: In function ‘setup_ctxt’: ./include/linux/build_bug.h:16:51: error: negative width in bit-field ‘’ #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); }))) ^ ./include/linux/compiler.h:258:28: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’ #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) ^~~~~~~~~~~~~~~~~ ./include/linux/sched.h:1941:23: note: in expansion of macro ‘__must_be_array’ __get_task_comm(buf, __must_be_array(buf) + sizeof(buf), tsk) ^~~~~~~~~~~~~~~ drivers/infiniband/hw/qib/qib_file_ops.c:1325:2: note: in expansion of macro ‘get_task_comm’ get_task_comm(test, current); It hit this warnig as expected. Suggested-by: Kees Cook Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- include/linux/sched.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index c1a927ddec64..b9c85c52fed0 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1931,10 +1931,8 @@ static inline void set_task_comm(struct task_struct *tsk, const char *from) } extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk); -#define get_task_comm(buf, tsk) ({ \ - BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \ - __get_task_comm(buf, sizeof(buf), tsk); \ -}) +#define get_task_comm(buf, tsk) \ + __get_task_comm(buf, __must_be_array(buf) + sizeof(buf), tsk) #ifdef CONFIG_SMP static __always_inline void scheduler_ipi(void)