From patchwork Wed Sep 27 19:50:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 9974661 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 463A2603F2 for ; Wed, 27 Sep 2017 19:53:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 397F62930D for ; Wed, 27 Sep 2017 19:53:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2E6B02930C; Wed, 27 Sep 2017 19:53:14 +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=ham 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 C5F512930E for ; Wed, 27 Sep 2017 19:53:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752114AbdI0TwZ (ORCPT ); Wed, 27 Sep 2017 15:52:25 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:37092 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751997AbdI0TvP (ORCPT ); Wed, 27 Sep 2017 15:51:15 -0400 Received: by mail-pf0-f193.google.com with SMTP id e69so7572782pfg.4; Wed, 27 Sep 2017 12:51:15 -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=3Fq9r/haFbQSjnoljgXFPXlW5omMRFxw+saXneK4lEE=; b=qamKXCP6Nagym7uSKRzRq+tisooSIPKHtfcUM5VEOkkvNfiw/EKK0Q24ITaaQYjlNm oA5mKtZXz7L900mW5OgDzilodJ332AJM5mtnVYmXdGiXLgs5WrY1msWWqywXqzeh9Vdq 8wIbx685mBcNL83CiREY8nqU6N/xd8ig7sueEFJESmLFw+rHwcWoohkaljVXEPSpSg4K CXpgSdU1iSpHTIqa3Qi68fEz7w9DKjxE0LqMeZ1KW1CDyywJ2BMhgA/yTTmc1z34a2+z Zz6LLNSDLbuKPKxuOSGp79A/uDciOSBgBr+YGw0VNd2mTRrZrD+JIbrDePkjzjuP97HL 7BcA== 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=3Fq9r/haFbQSjnoljgXFPXlW5omMRFxw+saXneK4lEE=; b=UBV359a6sRN+T7VF2QzjAfLuGfuDk4lBLy5BT3AF5nxB02/zZLQutC08EYPtqagFc8 K/GIWsNEmk7sCav5ULbLUImU106GCN+j7++7B4jYNYtJ3QANoJFqs5QL+8QU3viDkg9E Cvn574gqj7k0bjaWe2TwGhFKdW41wIdp/Oaz5FMVljgxVgWd4DYizND8J0pBLZjNjeBX MFMtBE8LKMR4BmwkyDc/qaPf/NRubVSg0YxYAolFwfKcgUhK46ggyCwFVPVCOSeDIDvs zUzQXdMhacGSebc8/+jtpFX3GM7lvDMMfwPRqVC2SqxigpLdv8f/29NxDc/LGHlGfrGt nH1A== X-Gm-Message-State: AHPjjUgQCT3PmwcgUPFLqb7viw2YBD6Nr4SE14wCJxe7oCKHE4U+p0/h Vi9fbexb8n+pfzzghMK4rY+yzGAk X-Google-Smtp-Source: AOwi7QC0dZBIl4DF4Ei/AKHI64MBTur4YZI+0qSWY8IHGn5ScQs+zvHw0LWpqaUFHdcIuM7rsd1BPQ== X-Received: by 10.99.112.79 with SMTP id a15mr2210392pgn.154.1506541874744; Wed, 27 Sep 2017 12:51:14 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.174.81]) by smtp.gmail.com with ESMTPSA id d5sm24954499pfg.26.2017.09.27.12.51.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 27 Sep 2017 12:51:14 -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 v3 4/7] KEYS: load key flags and expiry time atomically in key_validate() Date: Wed, 27 Sep 2017 12:50:44 -0700 Message-Id: <20170927195047.122358-5-ebiggers3@gmail.com> X-Mailer: git-send-email 2.14.2.822.g60be5d43e6-goog In-Reply-To: <20170927195047.122358-1-ebiggers3@gmail.com> References: <20170927195047.122358-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 key_validate(), load the flags and expiry time once atomically, since these can change concurrently if key_validate() is called without the key semaphore held. And we don't want to get inconsistent results if a variable is referenced multiple times. For example, key->expiry was referenced in both 'if (key->expiry)' and in 'if (now.tv_sec >= key->expiry)', making it theoretically possible to see a spurious EKEYEXPIRED while the expiration time was being removed, i.e. set to 0. Signed-off-by: Eric Biggers --- security/keys/permission.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/security/keys/permission.c b/security/keys/permission.c index 732cc0beffdf..a72b4dd70c8a 100644 --- a/security/keys/permission.c +++ b/security/keys/permission.c @@ -88,7 +88,8 @@ EXPORT_SYMBOL(key_task_permission); */ int key_validate(const struct key *key) { - unsigned long flags = key->flags; + unsigned long flags = READ_ONCE(key->flags); + time_t expiry = READ_ONCE(key->expiry); if (flags & (1 << KEY_FLAG_INVALIDATED)) return -ENOKEY; @@ -99,9 +100,9 @@ int key_validate(const struct key *key) return -EKEYREVOKED; /* check it hasn't expired */ - if (key->expiry) { + if (expiry) { struct timespec now = current_kernel_time(); - if (now.tv_sec >= key->expiry) + if (now.tv_sec >= expiry) return -EKEYEXPIRED; }