Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp913976imj; Fri, 15 Feb 2019 08:52:43 -0800 (PST) X-Google-Smtp-Source: AHgI3IaNGp1Ejedpkhpvnki+SYZ5RI5Yvom//wL3qV0TtwdQ0YpIe2IigfLyEOnbac5stxJACBD1 X-Received: by 2002:a62:3990:: with SMTP id u16mr10735257pfj.80.1550249563400; Fri, 15 Feb 2019 08:52:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550249563; cv=none; d=google.com; s=arc-20160816; b=vwj/PY3Au+fKG2vHZ/LiJpkkpmGsxmhTEro+35CYfyC6aayV1FsIpxXtCBuh7XrYWF yriBvh127k0L10kyosGIlccOw5MZz5z4+Tj+Wx+7JQT+seTO9ASus5KDU00KA2XKlcBG XyxrAlW7ijyOjr63AeSe3Mn8QQTTTE3XahPpwbFzD5VDdqXFngfwSrHX4LBfKGe+ptf0 V0ZQLAth+Hbf8cMf/R/CB5SEh3fUJSqkhS3EZYChuqTz0Zfcd7kRV92Ljtzoe+7Swp0Y KM87q8g34pm4qzfAlsXuYtJ/VgNDs46zBvssu7wnCPdKnqoIs0w1TKfbjh0HyMUpy0Up R1Qw== 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 :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:organization; bh=kK/c8K7kPO6Ve7gjnqeb6lKNZbAm9YZrWMWLd+5NpvU=; b=PTlr5HpZfl7GykerYok0X6t61tDg4RL8mWgxTMdMzXS43IbYSxQzjALxC4LYf+lJek +UTDn7igqg5w8Yuh9+yTxVFvuDzFY4u1+PvNkRifR157eYdRbIN8aG1kgnkHzIePuWJW 8QhOoKB7YY4gEWru2fk4OEcN9J8z7HRAuQ16IGYBOozysgPQa2I96WiL0LyyETqH+Qz+ vEFqR2SqVtzlGt+xriLdID+NAprroKP+/zDCP7lhfTlHonf6Z0xJRgL2Xv+EJtZaY12d cco6a53ifgZo15xfaJvPrybzX9MA7R4uQJINjEsue6PZt1/uCUawarKQRGMnQivJkbs6 1qjg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ck6si7270095plb.298.2019.02.15.08.52.23; Fri, 15 Feb 2019 08:52:43 -0800 (PST) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404144AbfBOQJg (ORCPT + 99 others); Fri, 15 Feb 2019 11:09:36 -0500 Received: from mx1.redhat.com ([209.132.183.28]:9999 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403968AbfBOQJf (ORCPT ); Fri, 15 Feb 2019 11:09:35 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8C2D2A12AA; Fri, 15 Feb 2019 16:09:35 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-121-129.rdu2.redhat.com [10.10.121.129]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8FD201024932; Fri, 15 Feb 2019 16:09:33 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [RFC PATCH 14/27] keys: Break bits out of key_unlink() From: David Howells To: keyrings@vger.kernel.org, trond.myklebust@hammerspace.com, sfrench@samba.org Cc: linux-security-module@vger.kernel.org, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, rgb@redhat.com, dhowells@redhat.com, linux-kernel@vger.kernel.org Date: Fri, 15 Feb 2019 16:09:32 +0000 Message-ID: <155024697274.21651.2339284853609462143.stgit@warthog.procyon.org.uk> In-Reply-To: <155024683432.21651.14153938339749694146.stgit@warthog.procyon.org.uk> References: <155024683432.21651.14153938339749694146.stgit@warthog.procyon.org.uk> User-Agent: StGit/unknown-version MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Fri, 15 Feb 2019 16:09:35 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Break bits out of key_unlink() into helper functions so that they can be used in implementing key_move(). Signed-off-by: David Howells --- security/keys/keyring.c | 89 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/security/keys/keyring.c b/security/keys/keyring.c index 062cad635edf..431094c6cd74 100644 --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -1409,6 +1409,66 @@ int key_link(struct key *keyring, struct key *key) } EXPORT_SYMBOL(key_link); +/* + * Begin the process of unlinking a key from a keyring. + */ +static int __key_unlink_begin(struct key *keyring, unsigned int lock_nesting, + struct key *key, struct assoc_array_edit **_edit) + __acquires(&keyring->sem) +{ + struct assoc_array_edit *edit; + int ret; + + if (keyring->type != &key_type_keyring) + return -ENOTDIR; + + down_write_nested(&keyring->sem, lock_nesting); + + edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops, + &key->index_key); + if (IS_ERR(edit)) { + ret = PTR_ERR(edit); + goto error; + } + + if (!edit) { + ret = -ENOENT; + goto error; + } + + *_edit = edit; + return 0; + +error: + up_write(&keyring->sem); + return ret; +} + +/* + * Apply an unlink change. + */ +static void __key_unlink(struct key *keyring, struct key *key, + struct assoc_array_edit **_edit) +{ + assoc_array_apply_edit(*_edit); + *_edit = NULL; + notify_key(keyring, NOTIFY_KEY_UNLINKED, key_serial(key)); + key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES); +} + +/* + * Finish unlinking a key from to a keyring. + */ +static void __key_unlink_end(struct key *keyring, + struct key *key, + struct assoc_array_edit *edit) + __releases(&keyring->sem) +{ + if (edit) + assoc_array_cancel_edit(edit); + up_write(&keyring->sem); +} + /** * key_unlink - Unlink the first link to a key from a keyring. * @keyring: The keyring to remove the link from. @@ -1429,35 +1489,18 @@ EXPORT_SYMBOL(key_link); int key_unlink(struct key *keyring, struct key *key) { struct assoc_array_edit *edit; - key_serial_t target = key_serial(key); int ret; key_check(keyring); key_check(key); - if (keyring->type != &key_type_keyring) - return -ENOTDIR; - - down_write(&keyring->sem); - - edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops, - &key->index_key); - if (IS_ERR(edit)) { - ret = PTR_ERR(edit); - goto error; - } - ret = -ENOENT; - if (edit == NULL) - goto error; - - assoc_array_apply_edit(edit); - notify_key(keyring, NOTIFY_KEY_UNLINKED, target); - key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES); - ret = 0; + ret = __key_unlink_begin(keyring, 0, key, &edit); + if (ret < 0) + return ret; -error: - up_write(&keyring->sem); - return ret; + __key_unlink(keyring, key, &edit); + __key_unlink_end(keyring, key, edit); + return 0; } EXPORT_SYMBOL(key_unlink);