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: 12595795 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 88E16C433F5 for ; Mon, 1 Nov 2021 06:04:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 72CA360EBC for ; Mon, 1 Nov 2021 06:04:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230460AbhKAGH3 (ORCPT ); Mon, 1 Nov 2021 02:07:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230396AbhKAGHJ (ORCPT ); Mon, 1 Nov 2021 02:07:09 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA417C061746; Sun, 31 Oct 2021 23:04:36 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id x16-20020a17090a789000b001a69735b339so614987pjk.5; Sun, 31 Oct 2021 23:04:36 -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=cQ1F9IaxwbEGSb72WC/p4WoxODTWncYrTKV7fctFjOBdFJpO+uZNCHPlnlfwHxt/eL I8vBjnP5hDABeYdeeqvw1oFMLEMZbuXNHg0x05/zjUfVtM4iytRc6L3ziK4mBD/Ez45V +2kjeUmS5kcCcTWXMCQAwscqLs8Xhvl7f+6qkwbAzm4IsBnDP9eyyPJ9uYC6z2rRMWdv G3gJsNKJji4oV3dYG85X4qNi8VEvzivPvkTx1v32vMyZV5uLfCNcvtIq7cNEpHWkh9E2 XidZqT/pBp7Ni0yZGo9+DJw5t7kMjtOuDrgRKtpwIDHxozaah6J1m3bCGjbRDW70Sqcp OYsw== X-Gm-Message-State: AOAM532Y5xa0sSlQbseIlBPXpUe+4zNm21k3Yo5pEsvn03DRQpnw24pB aQ7OD3ItrunuU10d9lTt7yk= 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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)