From patchwork Mon Jan 8 21:36:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Garrett X-Patchwork-Id: 10150633 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 6C49E601BE for ; Mon, 8 Jan 2018 21:36:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 672C2201B0 for ; Mon, 8 Jan 2018 21:36:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5C11D28355; Mon, 8 Jan 2018 21:36:33 +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, 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 C749528458 for ; Mon, 8 Jan 2018 21:36:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757366AbeAHVgb (ORCPT ); Mon, 8 Jan 2018 16:36:31 -0500 Received: from mail-qt0-f202.google.com ([209.85.216.202]:42063 "EHLO mail-qt0-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756468AbeAHVga (ORCPT ); Mon, 8 Jan 2018 16:36:30 -0500 Received: by mail-qt0-f202.google.com with SMTP id v30so9755025qtg.9 for ; Mon, 08 Jan 2018 13:36:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:message-id:subject:from:to:cc; bh=NC9ZC8NMGQ2psv6Fp9g4NvYJV3UPFZujl6dtD9jIAEc=; b=CkqMW24Ppl0VbSeBQodpwKeGwfE9hMRSRZM/CqRTt6KP33nI1viYwiqd562uVM1Aps 19annO48M18pw99dMlmMXhvqqpyO7qcPgarMYhFjPcFPYTt8ybH9Egar6OM41V7bbRVC wcv7jpWBV5wTZTHsZGT+EKtgVwpw2nke3A7W1fI2i2+9XNgxKhTcdl59mvB8U6aRbGze B150YJA6YIGnRdzrY9OPuAsYCJpI0bhniDMpW4WwdwPtsEDUxA2mAGHcWvsKOn14WFJg 2HQq/iOSG5cu5vDQqH2piASq1DkVxvO+ErYsvDZF4WdZoX4dZiJLN4hrtN07LgHHDRXl DpIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:message-id:subject:from:to:cc; bh=NC9ZC8NMGQ2psv6Fp9g4NvYJV3UPFZujl6dtD9jIAEc=; b=h2MUz02LulswmTjwR4yuizLJ86uHorY5hLH50OIiTc0kZcGKIBRMEyV+8m78sDyLhP lcEpAkg771oJQ6geRgg/lWc5bIni/LKqQlgBxzlKxfsbtWkeOUeul9lyUziaWt+TxYQ6 SocH58+Bg5WwhOQwikxzmwHg0nWS1vKz+X+cXZc9JpXd2YL9BYulsl3bEeancsXJHmhA 1bMCEVpCvywFHo6Z3V7zV+TmV+FPl0hU9kyGMUQNv+yUz2/H62vQlJYW4c2hHihTCeqb 5e1IBc+QBP85tCHykfPxv5Zb/JmC5xigjMt3FTeMdchadgwi7eFvkAyw/2txAkk2Yf6S IiWw== X-Gm-Message-State: AKwxytfdw02ZJWZJgc4x0gS7Qdqivg90HywXCHKcBc1+oVnTdFLEeTiB PDnyGigULPGhXMkA3EK+pY0WMl0MnWNdXhLbZ/l9Ww== X-Google-Smtp-Source: ACJfBotykuj6O0pnuGXqebarLtvneOeRy2C/gJWMguTj6GVy8qs+gVZubcjJmDPunpIuchEdr+YW9ojmwWGmGTrAk5XF/g== MIME-Version: 1.0 X-Received: by 10.55.171.10 with SMTP id u10mr10247663qke.4.1515447389393; Mon, 08 Jan 2018 13:36:29 -0800 (PST) Date: Mon, 8 Jan 2018 13:36:19 -0800 Message-Id: <20180108213620.170042-1-mjg59@google.com> X-Mailer: git-send-email 2.16.0.rc0.223.g4a4ac83678-goog Subject: [PATCH V6 1/2] security: Add a cred_getsecid hook From: Matthew Garrett To: linux-integrity@vger.kernel.org Cc: james.l.morris@oracle.com, Matthew Garrett , Paul Moore , Stephen Smalley , Eric Paris , selinux@tycho.nsa.gov, Casey Schaufler , linux-security-module@vger.kernel.org, Mimi Zohar , Dmitry Kasatkin Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP For IMA purposes, we want to be able to obtain the prepared secid in the bprm structure before the credentials are committed. Add a cred_getsecid hook that makes this possible. Signed-off-by: Matthew Garrett Acked-by: Paul Moore Cc: Paul Moore Cc: Stephen Smalley Cc: Eric Paris Cc: selinux@tycho.nsa.gov Cc: Casey Schaufler Cc: linux-security-module@vger.kernel.org Cc: Mimi Zohar Cc: Dmitry Kasatkin Cc: linux-integrity@vger.kernel.org --- include/linux/lsm_hooks.h | 6 ++++++ include/linux/security.h | 1 + security/security.c | 7 +++++++ security/selinux/hooks.c | 6 ++++++ security/smack/smack_lsm.c | 18 ++++++++++++++++++ 5 files changed, 38 insertions(+) diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index 7161d8e7ee79..72932dabbaed 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -554,6 +554,10 @@ * @new points to the new credentials. * @old points to the original credentials. * Transfer data from original creds to new creds + * @cred_getsecid: + * Retrieve the security identifier of the cred structure @c + * @c contains the credentials, secid will be placed into @secid. + * In case of failure, @secid will be set to zero. * @kernel_act_as: * Set the credentials for a kernel service to act as (subjective context). * @new points to the credentials to be modified. @@ -1541,6 +1545,7 @@ union security_list_options { int (*cred_prepare)(struct cred *new, const struct cred *old, gfp_t gfp); void (*cred_transfer)(struct cred *new, const struct cred *old); + void (*cred_getsecid)(const struct cred *c, u32 *secid); int (*kernel_act_as)(struct cred *new, u32 secid); int (*kernel_create_files_as)(struct cred *new, struct inode *inode); int (*kernel_module_request)(char *kmod_name); @@ -1824,6 +1829,7 @@ struct security_hook_heads { struct list_head cred_free; struct list_head cred_prepare; struct list_head cred_transfer; + struct list_head cred_getsecid; struct list_head kernel_act_as; struct list_head kernel_create_files_as; struct list_head kernel_read_file; diff --git a/include/linux/security.h b/include/linux/security.h index 73f1ef625d40..5cfff15ac378 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -324,6 +324,7 @@ int security_cred_alloc_blank(struct cred *cred, gfp_t gfp); void security_cred_free(struct cred *cred); int security_prepare_creds(struct cred *new, const struct cred *old, gfp_t gfp); void security_transfer_creds(struct cred *new, const struct cred *old); +void security_cred_getsecid(const struct cred *c, u32 *secid); int security_kernel_act_as(struct cred *new, u32 secid); int security_kernel_create_files_as(struct cred *new, struct inode *inode); int security_kernel_module_request(char *kmod_name); diff --git a/security/security.c b/security/security.c index 1cd8526cb0b7..35cbd75844c2 100644 --- a/security/security.c +++ b/security/security.c @@ -1005,6 +1005,13 @@ void security_transfer_creds(struct cred *new, const struct cred *old) call_void_hook(cred_transfer, new, old); } +void security_cred_getsecid(const struct cred *c, u32 *secid) +{ + *secid = 0; + call_void_hook(cred_getsecid, c, secid); +} +EXPORT_SYMBOL(security_cred_getsecid); + int security_kernel_act_as(struct cred *new, u32 secid) { return call_int_hook(kernel_act_as, 0, new, secid); diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 8644d864e3c1..d3009c027de8 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3844,6 +3844,11 @@ static void selinux_cred_transfer(struct cred *new, const struct cred *old) *tsec = *old_tsec; } +static void selinux_cred_getsecid(const struct cred *c, u32 *secid) +{ + *secid = cred_sid(c); +} + /* * set the security data for a kernel service * - all the creation contexts are set to unlabelled @@ -6479,6 +6484,7 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(cred_free, selinux_cred_free), LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), + LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c index 14cc7940b36d..b27327ebb031 100644 --- a/security/smack/smack_lsm.c +++ b/security/smack/smack_lsm.c @@ -2049,6 +2049,23 @@ static void smack_cred_transfer(struct cred *new, const struct cred *old) /* cbs copy rule list */ } +/** + * smack_cred_getsecid - get the secid corresponding to a creds structure + * @c: the object creds + * @secid: where to put the result + * + * Sets the secid to contain a u32 version of the smack label. + */ +static void smack_cred_getsecid(const struct cred *c, u32 *secid) +{ + struct smack_known *skp; + + rcu_read_lock(); + skp = smk_of_task(c->security); + *secid = skp->smk_secid; + rcu_read_unlock(); +} + /** * smack_kernel_act_as - Set the subjective context in a set of credentials * @new: points to the set of credentials to be modified. @@ -4727,6 +4744,7 @@ static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(cred_free, smack_cred_free), LSM_HOOK_INIT(cred_prepare, smack_cred_prepare), LSM_HOOK_INIT(cred_transfer, smack_cred_transfer), + LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid), LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as), LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as), LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),