From patchwork Thu Apr 1 22:13:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 413664 Delivered-To: patch@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp942231jai; Thu, 1 Apr 2021 15:13:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx3TW0QVA57hCVQWQ618vO9oZbgv8lxYrSubesxFDEZZWLudkcDV2+SvgbBJzU2hV9fK2cz X-Received: by 2002:a05:6e02:1d12:: with SMTP id i18mr8854075ila.238.1617315232824; Thu, 01 Apr 2021 15:13:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617315232; cv=none; d=google.com; s=arc-20160816; b=NQ8kJslyXJYvYqKmZjnhJguMfb6H113BZRFk3xwMWR4ch/4QZA4xaytrIIK0/YY0wO XvR57Dq/raHPYFwDbkZH7KFvcUb5EcfHUAfsbp3hNKCxBQlKnmqd8XlQmnBdQfyS1JFr /WzkgerMxoSfEXwzJB7w9Lch+NX3Dqa0x6abULQFKg9MFfWz5VoiadQH7jo3UT15xHbA 8o8O+sc4J1BY+oU7TasX2iqNkpPrK/BL1wCCpTvCYHVrMD+KFxshW29BpQNcqNdzJbrE hRVSyx0AKJ6HCp4t+lKSTjGyuvMJssrqYemycSo383W/aERw2usQv678uqdYeMuma+4l OFPg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+ed7VreEWmipFlx5jenlD6GzJkKJrcT4I8s/OqzA0i0=; b=Sy40tUiVg2UjtFqc33W7IS1mgU2CqsKoV/uKDbWSoZ1l4Tikg29Pp/Z661CDCMeWVN s7A6Ffv4EPr1aevDLYj2dUTT1JfLlSFLdxMf9vURG+rHbGa5vmPwlmkLVpAYZAZud9ig 5tFPXSgsNSc9MkG3g9BsALa4B7O5G7gKt0zhQJQoqTlUAEkbLcdOw3PcTiMrBY8E4M8+ 7aAhxq5pu8I/eMoipa/gPCoHGtLe+ZADn1LjenDi5jCjy/2gjJJOriEMJg0UHq0VVndC Qd5JcCJLGTF9Dj0T4OgpN5H+VPdeksN7xgHfeAdbdDUI2wTWf4H6+1Lx5Y0MWnYqN6FN nPSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MVcoIZrQ; spf=pass (google.com: domain of linux-kselftest-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kselftest-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z5si5380868ilh.73.2021.04.01.15.13.52; Thu, 01 Apr 2021 15:13:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kselftest-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MVcoIZrQ; spf=pass (google.com: domain of linux-kselftest-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kselftest-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234832AbhDAWNv (ORCPT + 5 others); Thu, 1 Apr 2021 18:13:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236110AbhDAWNc (ORCPT ); Thu, 1 Apr 2021 18:13:32 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2F52C061797 for ; Thu, 1 Apr 2021 15:13:31 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id j25so2434993pfe.2 for ; Thu, 01 Apr 2021 15:13:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+ed7VreEWmipFlx5jenlD6GzJkKJrcT4I8s/OqzA0i0=; b=MVcoIZrQlvVcmQw6Q7zTf+TL+LvBevTMEQM/sQTCGQdJRCHkODdqFrhlGlyLtPQqO1 MqHhedF01mipbWimf0tiEub58/KFZfgwlOONe0rNFLv51Kzf9OKCuFr4BVNfwnOa7TvX 3RuK52nBjEPN4VfjR6vNPNYljfKr3XLRrUlBQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+ed7VreEWmipFlx5jenlD6GzJkKJrcT4I8s/OqzA0i0=; b=BTfJOIECAERjPJdr0Pj+YMd3xXFH3fIqXU87kJELiYSxt2qICJfeWyEVFwj40znD19 kxRJPlPpfcPt8DIM708GdeqcrYQ3cEj4wWuCawTh4w7s1IFbNOD1bycGparnjl/DmTHm djrUg4aWZ1p/Z+m5HrjvRmtnPvKfIH7bzEZt+IKLPDm2RmkIbGRMur24H83qkDopgs/H AX4qC8MJnMpwLS/AsN5gfejR0CcxomMkPo3jRhrQ9FhTGwRrazv7EQTj3fQETmNWvbyE 8T8He/ZrWV2w5Cjk5SixA0Uvru5jiFqYNZr6Yz1eUKs8XbSsAYhS2xBsOCpQbgYfhJxO MQ1A== X-Gm-Message-State: AOAM531BaZOieUppiM6vZOBCWI9nsjpEkci23a3+kgwvveEW+TQ4gh0l +Bk8EmXpuigohft0Q9opS7PuUQ== X-Received: by 2002:a62:2742:0:b029:222:b711:3324 with SMTP id n63-20020a6227420000b0290222b7113324mr9372178pfn.7.1617315211421; Thu, 01 Apr 2021 15:13:31 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id fr23sm6380245pjb.22.2021.04.01.15.13.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Apr 2021 15:13:29 -0700 (PDT) From: Kees Cook To: Greg Kroah-Hartman Cc: Kees Cook , Arnd Bergmann , Christoph Hellwig , Nathan Chancellor , Tejun Heo , Alexander Viro , "Rafael J. Wysocki" , Shuah Khan , Nathan Chancellor , Nick Desaulniers , Andrew Morton , Kefeng Wang , "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org, clang-built-linux@googlegroups.com, Michal Hocko , Alexey Dobriyan , Lee Duncan , Chris Leech , Adam Nichols , linux-hardening@vger.kernel.org Subject: [PATCH v4 2/3] seq_file: Fix clang warning for NULL pointer arithmetic Date: Thu, 1 Apr 2021 15:13:19 -0700 Message-Id: <20210401221320.2717732-3-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210401221320.2717732-1-keescook@chromium.org> References: <20210401221320.2717732-1-keescook@chromium.org> MIME-Version: 1.0 X-Patch-Hashes: v=1; h=sha256; g=072ba8d0292080bbec63f540b8a7b8f4388d46e0; i=X28+TLSj6UAzyfUwM6ASBpL0O+6znj4kpkRLU6W477w=; m=BVZ7UGjppHc9pxpbNOtE+k2Mc/TPPXIP/bm3a5RPhjA=; p=bMQkNyH1GTm6eEjiE8Zl6RhCNwohxteBG+pqB7gCs1I= X-Patch-Sig: m=pgp; i=arnd@arndb.de; s=0x0x8972F4DFDC6DC026; b=iQIzBAABCgAdFiEEpcP2jyKd1g9yPm4TiXL039xtwCYFAmBmRX8ACgkQiXL039xtwCaLMg/+Lhx pOKAP38pEErrYMo+5DOvXlMaNUoctJ8O82UArlKz1tK0WnGh/2r6wsUb57mBRxiEm1Fqs4js4duAl kR7kxnB0PYJgEVpDX2mbErnsMYYEpTK2OPrFPfuXpogkv7ZzBTR8dfQwxETzu+yBfjnn1DmRNqIVF j9TrK2uBIE7lwsIWWyEuuEVYoZ68xhrJ9IrQ1ZLE5fA4VF/woqhbJKXQAUimIgUO6mvg9OJpISbWs CYQxi2njTOqAtu8mSsyhsuBuW8KV+G/PeM0oSqythjF6LKzULC86d3WEH6aEazn2znU7/frImFMxl UhkTHmOfkr/tMJyxO8jDuPjpXytP0fmIkpOSgNIh7PlQgLWXf5+OkbCoU/WSTY1jR27M7+HAvGd7Q C9hYO6odGUvbXgyGVaruHYEZfTQ/1Mym/duFmytroX4JG8bnj5gVv9L01vETRRa9WSeP6Fj8vzxoA ko6fy/Oq77nJ2SiA//Z1h3wNCzGagcTHRAh96xkDD1FzytY3Hjb8tbTwBZsYvlmvjQnyscKPBUPHc N8EC5Bc7vZPFKzPf53UMw/NirSq1etb50K5J7aWmAbmgJm+qryafE51E/Xvd/uaOrzjdypKRhuxGN uCHmvLBr6U1WaTio88kV78F55LIb9pgl0iVP+4UV7wscGidMpLzJGIVSeO8qaN5w= Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Arnd Bergmann Clang points out that adding something to NULL is not allowed in standard C: fs/kernfs/file.c:127:15: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] return NULL + !*ppos; ~~~~ ^ fs/seq_file.c:529:14: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] return NULL + (*pos == 0); Rephrase the code to be extra explicit about the valid, giving them named SEQ_OPEN_EOF and SEQ_OPEN_SINGLE definitions. The instance in kernfs was copied from single_start, so fix both at once. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Fixes: c2b19daf6760 ("sysfs, kernfs: prepare read path for kernfs") Reviewed-by: Christoph Hellwig Reviewed-by: Nathan Chancellor Signed-off-by: Arnd Bergmann Signed-off-by: Kees Cook Link: https://lore.kernel.org/r/20201028151202.3074398-1-arnd@kernel.org --- fs/kernfs/file.c | 9 ++++++--- fs/seq_file.c | 5 ++++- include/linux/seq_file.h | 6 ++++++ 3 files changed, 16 insertions(+), 4 deletions(-) -- 2.25.1 diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index c75719312147..721bcbc1d4d0 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -122,10 +122,13 @@ static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos) return next; } else { /* - * The same behavior and code as single_open(). Returns - * !NULL if pos is at the beginning; otherwise, NULL. + * The same behavior and code as single_open(). Continues + * if pos is at the beginning; otherwise, NULL. */ - return NULL + !*ppos; + if (*ppos) + return NULL; + + return SEQ_OPEN_SINGLE; } } diff --git a/fs/seq_file.c b/fs/seq_file.c index cb11a34fb871..1b5bd95d0a48 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -542,7 +542,10 @@ EXPORT_SYMBOL(seq_dentry); static void *single_start(struct seq_file *p, loff_t *pos) { - return NULL + (*pos == 0); + if (*pos) + return NULL; + + return SEQ_OPEN_SINGLE; } static void *single_next(struct seq_file *p, void *v, loff_t *pos) diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index b83b3ae3c877..51c870765bfd 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -37,6 +37,12 @@ struct seq_operations { #define SEQ_SKIP 1 +/* + * op->start must return a non-NULL pointer for single_open(), + * this is used when we don't care about the specific value. + */ +#define SEQ_OPEN_SINGLE ((void *)1) + /** * seq_has_overflowed - check if the buffer has overflowed * @m: the seq_file handle