From patchwork Wed Mar 27 14:53:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 10873633 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 30E3E1390 for ; Wed, 27 Mar 2019 14:54:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1B983287CE for ; Wed, 27 Mar 2019 14:54:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0FB8628BE9; Wed, 27 Mar 2019 14:54:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 830F928BE6 for ; Wed, 27 Mar 2019 14:54:05 +0000 (UTC) Received: (qmail 27994 invoked by uid 550); 27 Mar 2019 14:54:01 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 27966 invoked from network); 27 Mar 2019 14:54:00 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LsF4MOIT4z4fdfwNAyuntvIwt6iP7atPYvwA3arGD14=; b=BBpgm1NlnZhKSH87DRVYI3AiOhzjJ/q+7CpqM13H6AK9SrAM7t03D4sy3ofzfxOO85 xX1bvplar5BK2gUlCy4BuHy4C17duVGawvs1myHFzfpTp3CCMofjJDM1Vo+vYeXYyPj9 xB3lwuLqvozSSgMgkd4ETIQ5ehAr+QKLO+Cn0= 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:mime-version :content-transfer-encoding; bh=LsF4MOIT4z4fdfwNAyuntvIwt6iP7atPYvwA3arGD14=; b=JxsHYcnamuTNd1bn51fEgJjGnxnfYZB3jMV4TozC/m+Is+hYhEQrGflqYSMn9QKfPW mstJ3E5JBNem/T5DDoKsd4/Aesg9wAdDUQQStgV2YsnV7PFO3FxczAframNSWqBhWPDq ugOSPB6nzUvVSfAOgdaIhQlFZQz8F+RFEDJns1SUuRIYkKwRHnvgdB4HlKz3OIXGjqzA WLebPQ5gYVxu/lyHOQCEpA0x5kqZOvtn1h1Sn40v3k8QcLwso0ucHgUYACpERJNPNO1p y7A2jSg21ijIcCxF+wCwPIUl0TSyoj2+gkisJqaHKrxDHOqOW3CJ5nvf0+obMokp6DeZ VQhw== X-Gm-Message-State: APjAAAU27dmomOWyOGLgNUGxfFDBQ41at2xw5G9Pcllw5wvIFCynScF+ LfheYsMhq4liwWCTELcCHZdVgw== X-Google-Smtp-Source: APXvYqyyiDNB/7Nz2PGpQaojBQJUROXMahgJR1nQmWE9UUZ8H0XmI2JbT58UtGX3OLjtB/wc6WTgYA== X-Received: by 2002:a63:28f:: with SMTP id 137mr8252384pgc.377.1553698427990; Wed, 27 Mar 2019 07:53:47 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , keescook@chromium.org, kernel-team@android.com, kernel-hardening@lists.openwall.com, Andrew Morton , "Eric W. Biederman" , Matthew Wilcox , Michal Hocko , Oleg Nesterov Subject: [PATCH] Convert struct pid count to refcount_t Date: Wed, 27 Mar 2019 10:53:31 -0400 Message-Id: <20190327145331.215360-1-joel@joelfernandes.org> X-Mailer: git-send-email 2.21.0.392.gf8f6787159e-goog MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP struct pid's count is an atomic_t field used as a refcount. Use refcount_t for it which is basically atomic_t but does additional checking to prevent use-after-free bugs. No change in behavior if CONFIG_REFCOUNT_FULL=n. Cc: keescook@chromium.org Cc: kernel-team@android.com Cc: kernel-hardening@lists.openwall.com Signed-off-by: Joel Fernandes (Google) --- include/linux/pid.h | 5 +++-- kernel/pid.c | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/linux/pid.h b/include/linux/pid.h index 14a9a39da9c7..8cb86d377ff5 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -3,6 +3,7 @@ #define _LINUX_PID_H #include +#include enum pid_type { @@ -56,7 +57,7 @@ struct upid { struct pid { - atomic_t count; + refcount_t count; unsigned int level; /* lists of tasks that use this pid */ struct hlist_head tasks[PIDTYPE_MAX]; @@ -69,7 +70,7 @@ extern struct pid init_struct_pid; static inline struct pid *get_pid(struct pid *pid) { if (pid) - atomic_inc(&pid->count); + refcount_inc(&pid->count); return pid; } diff --git a/kernel/pid.c b/kernel/pid.c index 20881598bdfa..2095c7da644d 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #include @@ -106,8 +106,8 @@ void put_pid(struct pid *pid) return; ns = pid->numbers[pid->level].ns; - if ((atomic_read(&pid->count) == 1) || - atomic_dec_and_test(&pid->count)) { + if ((refcount_read(&pid->count) == 1) || + refcount_dec_and_test(&pid->count)) { kmem_cache_free(ns->pid_cachep, pid); put_pid_ns(ns); } @@ -210,7 +210,7 @@ struct pid *alloc_pid(struct pid_namespace *ns) } get_pid_ns(ns); - atomic_set(&pid->count, 1); + refcount_set(&pid->count, 1); for (type = 0; type < PIDTYPE_MAX; ++type) INIT_HLIST_HEAD(&pid->tasks[type]);