From patchwork Tue Sep 26 20:11:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9972685 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id ED0DE602BD for ; Tue, 26 Sep 2017 20:16:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E271728BD9 for ; Tue, 26 Sep 2017 20:16:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D778328F99; Tue, 26 Sep 2017 20:16:30 +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=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8E62528BD9 for ; Tue, 26 Sep 2017 20:16:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965798AbdIZUQQ (ORCPT ); Tue, 26 Sep 2017 16:16:16 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:38730 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968792AbdIZUOd (ORCPT ); Tue, 26 Sep 2017 16:14:33 -0400 Received: by mail-pf0-f195.google.com with SMTP id a7so5414790pfj.5; Tue, 26 Sep 2017 13:14:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8cYOpnSscINDz50hY9BQ3mbggE0ZCYAcga7t43T3lKI=; b=BdgsxUtAPxcTYu1vOqqgP+1Msita8IstLeDwZbms21GU4zylbN+MY7dgvFxjDWqCNt RQxRCWt2O+HtdIwiRTxyT50ew6VeD/Gfz3JKsEB35Kx8um5DTQiuiNNWI7uWo6qoV9tN Nq5Q36zaRr32ckOBl932ksmnA0zOoyAu6+f9IGDRLRBjTYgCZx0h3ECTrPmUez1r/IlA eN0C93V96HUn4p7K6sfZCZ7gvaejktV1eqaV0CM32TZFkg5ZkATVY4aJx9N+FMXFuBQ7 cGTs4tRrQHy22TzZfceF6YXHqYAiVAiFLnuf8HxR9lNCcYGZZVyxy1TjLj3+cDMSgrcZ R/mA== 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; bh=8cYOpnSscINDz50hY9BQ3mbggE0ZCYAcga7t43T3lKI=; b=BWXGlNK/CC9U6mEC+czHdNmYeBhdjHdw4oySDngq0PCd868+Gr5qaWuVdF7pKKhDmw MEf1qzJlarFgelLzVhBtMDMGMe8EznSqPF7bbOdeNo0iLLGkXpCnaQPiw8Yhd6XgAp56 xXJXd5aZiTDaoeFEz0cza1FolutvawEjNnF6wx/mh3iD0GTrXV48SWsU1rcsqxiEqbna ErEnVon3i0dMvtm+5d+m3aCpBTy2IcUO4q5td4ptyodEaHwy5aQzqANbQGj5CaYDOoH4 udlSZTT04KuOQ+V7gnXmAegJODlT39qqMeecCK6JiepHyoCIua/W8ap7+muDp4M8SehH kJcw== X-Gm-Message-State: AHPjjUjM3Pr9MaaIXCxAVFQ83XH+keYekCEypB8n/a5Ae/avDQkGIXQM V9a01L7RQn231MJh/QKaN01ljxKk X-Google-Smtp-Source: AOwi7QCWZoyP+fGEfTFmjB8p0pAVg/GvFNMMRnLISTwFNSi4nfCvqof5R4XcSzEkqzBzbPQ3j56DHw== X-Received: by 10.84.129.193 with SMTP id b59mr12007108plb.200.1506456872414; Tue, 26 Sep 2017 13:14:32 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.174.81]) by smtp.gmail.com with ESMTPSA id g68sm16597640pfc.64.2017.09.26.13.14.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Sep 2017 13:14:32 -0700 (PDT) From: Eric Biggers To: keyrings@vger.kernel.org Cc: David Howells , Michael Halcrow , linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Biggers Subject: [PATCH v2 5/6] KEYS: load key flags and expiry time atomically in proc_keys_show() Date: Tue, 26 Sep 2017 13:11:04 -0700 Message-Id: <20170926201105.126166-6-ebiggers3@gmail.com> X-Mailer: git-send-email 2.14.1.992.g2c7b836f3a-goog In-Reply-To: <20170926201105.126166-1-ebiggers3@gmail.com> References: <20170926201105.126166-1-ebiggers3@gmail.com> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers In proc_keys_show(), the key semaphore is not held, so the key ->flags and ->expiry can be changed concurrently. We therefore should read them atomically just once. Otherwise /proc/keys may show an inconsistent state, such a key that is negative ('N') but not instantiated ('I'). Signed-off-by: Eric Biggers --- security/keys/proc.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/security/keys/proc.c b/security/keys/proc.c index de834309d100..a038069ac46a 100644 --- a/security/keys/proc.c +++ b/security/keys/proc.c @@ -179,7 +179,9 @@ static int proc_keys_show(struct seq_file *m, void *v) struct rb_node *_p = v; struct key *key = rb_entry(_p, struct key, serial_node); struct timespec now; + time_t expiry; unsigned long timo; + unsigned long flags; key_ref_t key_ref, skey_ref; char xbuf[16]; int rc; @@ -217,12 +219,13 @@ static int proc_keys_show(struct seq_file *m, void *v) rcu_read_lock(); /* come up with a suitable timeout value */ - if (key->expiry == 0) { + expiry = READ_ONCE(key->expiry); + if (expiry == 0) { memcpy(xbuf, "perm", 5); - } else if (now.tv_sec >= key->expiry) { + } else if (now.tv_sec >= expiry) { memcpy(xbuf, "expd", 5); } else { - timo = key->expiry - now.tv_sec; + timo = expiry - now.tv_sec; if (timo < 60) sprintf(xbuf, "%lus", timo); @@ -236,18 +239,19 @@ static int proc_keys_show(struct seq_file *m, void *v) sprintf(xbuf, "%luw", timo / (60*60*24*7)); } -#define showflag(KEY, LETTER, FLAG) \ - (test_bit(FLAG, &(KEY)->flags) ? LETTER : '-') +#define showflag(FLAGS, LETTER, FLAG) \ + ((FLAGS & (1 << FLAG)) ? LETTER : '-') + flags = READ_ONCE(key->flags); seq_printf(m, "%08x %c%c%c%c%c%c%c %5d %4s %08x %5d %5d %-9.9s ", key->serial, - showflag(key, 'I', KEY_FLAG_INSTANTIATED), - showflag(key, 'R', KEY_FLAG_REVOKED), - showflag(key, 'D', KEY_FLAG_DEAD), - showflag(key, 'Q', KEY_FLAG_IN_QUOTA), - showflag(key, 'U', KEY_FLAG_USER_CONSTRUCT), - showflag(key, 'N', KEY_FLAG_NEGATIVE), - showflag(key, 'i', KEY_FLAG_INVALIDATED), + showflag(flags, 'I', KEY_FLAG_INSTANTIATED), + showflag(flags, 'R', KEY_FLAG_REVOKED), + showflag(flags, 'D', KEY_FLAG_DEAD), + showflag(flags, 'Q', KEY_FLAG_IN_QUOTA), + showflag(flags, 'U', KEY_FLAG_USER_CONSTRUCT), + showflag(flags, 'N', KEY_FLAG_NEGATIVE), + showflag(flags, 'i', KEY_FLAG_INVALIDATED), refcount_read(&key->usage), xbuf, key->perm,