Received: by 10.192.165.148 with SMTP id m20csp286349imm; Wed, 9 May 2018 12:41:30 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqmhh2faS7zppPzk7SjPYequ+aQ57aw4wIXHONgzlmEO2k0Dya3InRpgf2e+SAKcKaOtLrE X-Received: by 2002:a63:6307:: with SMTP id x7-v6mr5023585pgb.269.1525894889996; Wed, 09 May 2018 12:41:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525894889; cv=none; d=google.com; s=arc-20160816; b=QFDNQoSS41ECkHUw//iW7oo1TeFp97t3BwheqMe/9BZfsQW4T5C5Wy0IEvysPMv01B SsRr7E/vW9lFGinkM+cb32HqnWqaH47AMAJ/69iLJOEbBlqM23NfIonMuTYHpiBsVQfE sE+h3SmilfzrneyRTss/nSlfIixT5r2OXG/YAxaBzGI1m7UAH7Mk3c28vhUYw1JlFwZA cCAQ3UW9eAAqZUBAL/ZJm8BxNSziJNJuSxxfZFfo8Bd1JQtKR+cNQfGthq7TvUXdxPC+ 7mx7zbhsXHLivsDOPE1lxsMI3lT3X292GwmlW3gP0vbSnibDpWF1MtAzpZzgOrPUQGpX dkvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=d3vfYJw67tZ7TMzjVFFlf2v8m4jD9BG3KOs9u1SIyZw=; b=HZuOrKxmPMNJXwpkmACdiG+rZfQGvpZZs0NeB/cOVulzU5JyoumGL91EQZKp4Wvok2 KLzJK3ugn2x7prdxWgt868w8cENbFwmakczdMCgfDRHwHuJ+TlWtVzjewVxbi2KKcQs4 ZSNqRV7b0yli7QqPgdu+W1r7+N3COj2ZWD7wSa0w+i5Oek/hojM842N6LugbbA25MJ6M zmlMCf0RYCb4W7UBZfgK5iDLhDNmgKJtJFeqy/MLvtNJYtcNWrLJoFr74biXYxrnXOrz SYGiOj4BrLSVqEEBzkJAkfE2qwwqCE3Xhv3Bvdy37T9nStH3maeUkcVKRjeeFKLyuZRG w6Ow== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p73si24367981pfk.275.2018.05.09.12.41.15; Wed, 09 May 2018 12:41:29 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965629AbeEITiu (ORCPT + 99 others); Wed, 9 May 2018 15:38:50 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:34562 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965097AbeEITiq (ORCPT ); Wed, 9 May 2018 15:38:46 -0400 Received: from localhost ([127.0.0.1] helo=bazinga.breakpoint.cc) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1fGUv9-0005v9-Ju; Wed, 09 May 2018 21:38:39 +0200 From: Sebastian Andrzej Siewior To: linux-kernel@vger.kernel.org Cc: tglx@linutronix.de, Peter Zijlstra , Ingo Molnar , linux-mm@kvack.org, Shaohua Li , linux-raid@vger.kernel.org, Anna-Maria Gleixner , Sebastian Andrzej Siewior Subject: [PATCH 2/8] userns: use refcount_t for reference counting instead atomic_t Date: Wed, 9 May 2018 21:36:39 +0200 Message-Id: <20180509193645.830-3-bigeasy@linutronix.de> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180509193645.830-1-bigeasy@linutronix.de> References: <20180509193645.830-1-bigeasy@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org refcount_t type and corresponding API should be used instead of atomic_t wh= en the variable is used as a reference counter. This allows to avoid accidental refcounter overflows that might lead to use-after-free situations. Suggested-by: Peter Zijlstra Signed-off-by: Sebastian Andrzej Siewior --- include/linux/sched/user.h | 5 +++-- kernel/user.c | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/linux/sched/user.h b/include/linux/sched/user.h index 96fe289c4c6e..39ad98c09c58 100644 --- a/include/linux/sched/user.h +++ b/include/linux/sched/user.h @@ -4,6 +4,7 @@ =20 #include #include +#include #include =20 struct key; @@ -12,7 +13,7 @@ struct key; * Some day this will be a full-fledged user tracking system.. */ struct user_struct { - atomic_t __count; /* reference count */ + refcount_t __count; /* reference count */ atomic_t processes; /* How many processes does this user have? */ atomic_t sigpending; /* How many pending signals does this user have? */ #ifdef CONFIG_FANOTIFY @@ -59,7 +60,7 @@ extern struct user_struct root_user; extern struct user_struct * alloc_uid(kuid_t); static inline struct user_struct *get_uid(struct user_struct *u) { - atomic_inc(&u->__count); + refcount_inc(&u->__count); return u; } extern void free_uid(struct user_struct *); diff --git a/kernel/user.c b/kernel/user.c index 36288d840675..5f65ef195259 100644 --- a/kernel/user.c +++ b/kernel/user.c @@ -96,7 +96,7 @@ static DEFINE_SPINLOCK(uidhash_lock); =20 /* root_user.__count is 1, for init task cred */ struct user_struct root_user =3D { - .__count =3D ATOMIC_INIT(1), + .__count =3D REFCOUNT_INIT(1), .processes =3D ATOMIC_INIT(1), .sigpending =3D ATOMIC_INIT(0), .locked_shm =3D 0, @@ -123,7 +123,7 @@ static struct user_struct *uid_hash_find(kuid_t uid, st= ruct hlist_head *hashent) =20 hlist_for_each_entry(user, hashent, uidhash_node) { if (uid_eq(user->uid, uid)) { - atomic_inc(&user->__count); + refcount_inc(&user->__count); return user; } } @@ -170,7 +170,7 @@ void free_uid(struct user_struct *up) return; =20 local_irq_save(flags); - if (atomic_dec_and_lock(&up->__count, &uidhash_lock)) + if (refcount_dec_and_lock(&up->__count, &uidhash_lock)) free_user(up, flags); else local_irq_restore(flags); @@ -191,7 +191,7 @@ struct user_struct *alloc_uid(kuid_t uid) goto out_unlock; =20 new->uid =3D uid; - atomic_set(&new->__count, 1); + refcount_set(&new->__count, 1); ratelimit_state_init(&new->ratelimit, HZ, 100); ratelimit_set_flags(&new->ratelimit, RATELIMIT_MSG_ON_RELEASE); =20 --=20 2.17.0