Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 68d4867a authored by Will Deacon's avatar Will Deacon Committed by Bernhard Thoben
Browse files

locking/refcount: Ensure integer operands are treated as signed



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.

Signed-off-by: default avatarWill Deacon <will@kernel.org>
Reviewed-by: default avatarArd Biesheuvel <ardb@kernel.org>
Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Tested-by: default avatarHanjun Guo <guohanjun@huawei.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Elena Reshetova <elena.reshetova@intel.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lkml.kernel.org/r/20191121115902.2551-3-will@kernel.org


Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
(cherry picked from commit 97a1420adf0cdf0cf6f41bab0b2acf658c96b94b)
parent dbeda121
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -43,6 +43,18 @@ static inline unsigned int refcount_read(const refcount_t *r)
	return atomic_read(&r->refs);
}


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(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);

#ifdef CONFIG_REFCOUNT_FULL

#define REFCOUNT_MAX		(UINT_MAX - 1)
+3 −3
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@
 *
 * Return: false if the passed refcount is 0, true otherwise
 */
bool refcount_add_not_zero(int i, refcount_t *r)
bool refcount_add_not_zero_checked(int i, refcount_t *r)
{
	unsigned int new, val = atomic_read(&r->refs);

@@ -102,7 +102,7 @@ EXPORT_SYMBOL(refcount_add_not_zero);
 * cases, refcount_inc(), or one of its variants, should instead be used to
 * increment a reference count.
 */
void refcount_add(int i, refcount_t *r)
void refcount_add_checked(int i, refcount_t *r)
{
	WARN_ONCE(!refcount_add_not_zero(i, r), "refcount_t: addition on 0; use-after-free.\n");
}
@@ -181,7 +181,7 @@ EXPORT_SYMBOL(refcount_inc);
 *
 * Return: true if the resulting refcount is 0, false otherwise
 */
bool refcount_sub_and_test(int i, refcount_t *r)
bool refcount_sub_and_test_checked(int i, refcount_t *r)
{
	unsigned int new, val = atomic_read(&r->refs);