Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp4462451ybp; Mon, 7 Oct 2019 08:48:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqyy4q3SqXA54owvKUzvTtRbAB95a5Bk44NgWZRL84hblFMlTx0YulXJjzxrEXXgY9MrXRyH X-Received: by 2002:a17:906:8287:: with SMTP id h7mr24294030ejx.61.1570463281913; Mon, 07 Oct 2019 08:48:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570463281; cv=none; d=google.com; s=arc-20160816; b=NFPXNHmY6xcO52xBU4Uyg4KN2X3Bhx3GgIG7thULDaTvONZmRCy4kG3EHlcAk/LPXf +ty8Uvkdj7F2SefVhZLy4+GTT/yOL8KzH20PKYfzjT2BUAAYHCwmkitU40j6nqM2K6XU IghJIMedasN3sfC0niCvyAFj/3xcx3RSOSPu5gr3qXQhj3nSbg1b42ahMg7QDPAAxyOI oqK4BT0cfcS+SmKQ14xVuQy6tJWWaiFROZec+8ZRU5Zs9bqqGLJjF6c65jPeQGJcTDkM 1FFOYRiNkhYgNEbWQAPhF9mjCnK+boeWGxeGSyubVX6VcDgc7/VlBRh0Os92fD6suciD OgBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=2AjETE2+DAyNBk7G0lYPT1dRinnyqJkfco1vngxuWmY=; b=HoZPtZcifeWVXHQssmy8MuoZjNCxzSUP2az8OAlY+eDYtthQdG85Un2Hazc6aAmhv+ LvYk8t53EpW0yteeTCnXbLljx0kzcM42uuoSY+YD6cvdRK2InnuvHa4/3/sGj2OF/OUt lHsF3xbSlliEW5QgANVr3Vp3jhuaMEoDpKkr6XuvhmiijdSvjfIMpN0+jJYrmq2cOP2H 4vr3UY/t8yBqbyk3so4yWlNod0qFnKxvGfatYVH2QSI98qDOgbxVAMNEZOCBgvzf68UQ WLonH7XA1H56ifsnUGdbUNkjZDTX/wwHe/Dw5UAzUfrvWLNd3SpQAEW4ddo/ocIzpa0B 3LlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mF6NbSwe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b22si8985469eda.194.2019.10.07.08.47.37; Mon, 07 Oct 2019 08:48:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mF6NbSwe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728631AbfJGPrP (ORCPT + 99 others); Mon, 7 Oct 2019 11:47:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:51596 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727711AbfJGPrO (ORCPT ); Mon, 7 Oct 2019 11:47:14 -0400 Received: from localhost.localdomain (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CD93E20684; Mon, 7 Oct 2019 15:47:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570463233; bh=AUq6QvYEhIzfEPdMuWdKvqm6pRttvR3FgB+HbspmrLo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mF6NbSweUlpiZQtjSO/t3HmBd0K9JzHFQhYDr9k+8P1Yjhj7X96Y3S1OZuPLl7LFo ZDe6J8zWJ+S5RayUuTpn24rEhkeuCz15HEFAnH2wQcFzu62DqHcuSNV1rOQWepJsg/ V4W6HGq5VAdX6QQzHH5Twv7d30u7kQvsMhaVIfXA= From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Kees Cook , Ingo Molnar , Elena Reshetova , Peter Zijlstra , Ard Biesheuvel , Hanjun Guo , Jan Glauber Subject: [PATCH v3 02/10] lib/refcount: Ensure integer operands are treated as signed Date: Mon, 7 Oct 2019 16:46:55 +0100 Message-Id: <20191007154703.5574-3-will@kernel.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20191007154703.5574-1-will@kernel.org> References: <20191007154703.5574-1-will@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation for changing the saturation point of REFCOUNT_FULL to INT_MIN / 2, change the type of integer operands passed into the API from 'unsigned int' to 'int' so that we can avoid casting during comparisons when we don't want to fall foul of C integral conversion rules for signed and unsigned types. Since the kernel is compiled with '-fno-strict-overflow', we don't need to worry about the UB introduced by signed overflow here. Furthermore, we're already making heavy use of the atomic_t API, which operates exclusively on signed types. Cc: Ingo Molnar Cc: Elena Reshetova Cc: Peter Zijlstra Cc: Ard Biesheuvel Reviewed-by: Kees Cook Signed-off-by: Will Deacon --- include/linux/refcount.h | 14 +++++++------- lib/refcount.c | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/linux/refcount.h b/include/linux/refcount.h index 79f62e8d2256..89066a1471dd 100644 --- a/include/linux/refcount.h +++ b/include/linux/refcount.h @@ -28,7 +28,7 @@ typedef struct refcount_struct { * @r: the refcount * @n: value to which the refcount will be set */ -static inline void refcount_set(refcount_t *r, unsigned int n) +static inline void refcount_set(refcount_t *r, int n) { atomic_set(&r->refs, n); } @@ -44,13 +44,13 @@ static inline unsigned int refcount_read(const refcount_t *r) return atomic_read(&r->refs); } -extern __must_check bool refcount_add_not_zero_checked(unsigned int i, refcount_t *r); -extern void refcount_add_checked(unsigned int i, refcount_t *r); +extern __must_check bool refcount_add_not_zero_checked(int i, refcount_t *r); +extern void refcount_add_checked(int i, refcount_t *r); extern __must_check bool refcount_inc_not_zero_checked(refcount_t *r); extern void refcount_inc_checked(refcount_t *r); -extern __must_check bool refcount_sub_and_test_checked(unsigned int i, refcount_t *r); +extern __must_check bool refcount_sub_and_test_checked(int i, refcount_t *r); extern __must_check bool refcount_dec_and_test_checked(refcount_t *r); extern void refcount_dec_checked(refcount_t *r); @@ -79,12 +79,12 @@ extern void refcount_dec_checked(refcount_t *r); # ifdef CONFIG_ARCH_HAS_REFCOUNT # include # else -static inline __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r) +static inline __must_check bool refcount_add_not_zero(int i, refcount_t *r) { return atomic_add_unless(&r->refs, i, 0); } -static inline void refcount_add(unsigned int i, refcount_t *r) +static inline void refcount_add(int i, refcount_t *r) { atomic_add(i, &r->refs); } @@ -99,7 +99,7 @@ static inline void refcount_inc(refcount_t *r) atomic_inc(&r->refs); } -static inline __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r) +static inline __must_check bool refcount_sub_and_test(int i, refcount_t *r) { return atomic_sub_and_test(i, &r->refs); } diff --git a/lib/refcount.c b/lib/refcount.c index 48b78a423d7d..719b0bc42ab1 100644 --- a/lib/refcount.c +++ b/lib/refcount.c @@ -61,7 +61,7 @@ * * Return: false if the passed refcount is 0, true otherwise */ -bool refcount_add_not_zero_checked(unsigned int i, refcount_t *r) +bool refcount_add_not_zero_checked(int i, refcount_t *r) { unsigned int new, val = atomic_read(&r->refs); @@ -101,7 +101,7 @@ EXPORT_SYMBOL(refcount_add_not_zero_checked); * cases, refcount_inc(), or one of its variants, should instead be used to * increment a reference count. */ -void refcount_add_checked(unsigned int i, refcount_t *r) +void refcount_add_checked(int i, refcount_t *r) { WARN_ONCE(!refcount_add_not_zero_checked(i, r), "refcount_t: addition on 0; use-after-free.\n"); } @@ -180,7 +180,7 @@ EXPORT_SYMBOL(refcount_inc_checked); * * Return: true if the resulting refcount is 0, false otherwise */ -bool refcount_sub_and_test_checked(unsigned int i, refcount_t *r) +bool refcount_sub_and_test_checked(int i, refcount_t *r) { unsigned int new, val = atomic_read(&r->refs); -- 2.23.0.581.g78d2f28ef7-goog