From patchwork Thu Oct 19 23:14:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Garrett X-Patchwork-Id: 10018505 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 0B88460224 for ; Thu, 19 Oct 2017 23:14:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECFF928D12 for ; Thu, 19 Oct 2017 23:14:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EBB9B28E75; Thu, 19 Oct 2017 23:14:40 +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.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM 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 0A60528D12 for ; Thu, 19 Oct 2017 23:14:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751353AbdJSXOj (ORCPT ); Thu, 19 Oct 2017 19:14:39 -0400 Received: from mail-yw0-f201.google.com ([209.85.161.201]:43322 "EHLO mail-yw0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751112AbdJSXOi (ORCPT ); Thu, 19 Oct 2017 19:14:38 -0400 Received: by mail-yw0-f201.google.com with SMTP id w5so7875351ywg.2 for ; Thu, 19 Oct 2017 16:14:38 -0700 (PDT) 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=eEL9g7fWofR/WdZ3k+VAfQu77MSezqPqsJpicIOQt4w=; b=RigE4AcCBsQW38qXZZvvO6a7NyUiG9feshNmnpy9iWTxxC/YnP8QeeMyfQHygLRB+k XtgZpfWu1UkiO+73Jksm6Jofj3arqbctPDN+iM73R2RYn9nA7fBPSh7O/WTpfWEZmpTa 95Q+eow/WUpmcaSnM2+qgmYU+bBR7Wr6/6v0T+yJfIlaynQdlzvxxp5pXiPkbtiHADii 2mT+10uz8TzwZ0WZNlA1FENhtTZF/Bhxzp1ipjLgv0d+yf+LU8KWkYAY0v8YMTOlMxA2 BdVAjNkmWXx1EzA02ys47lirP08g5o8R7k+SOHuQZqAFf+Uk4/Q/ApPAbB/l64cnHepl /ogA== 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=eEL9g7fWofR/WdZ3k+VAfQu77MSezqPqsJpicIOQt4w=; b=S/Vv6Uj/SBTe+nwLFOXgSqPlFMzQ1EMafWQWT7lHTmqwwxH2Dbujr1+E82jy0hUS1i LeO+Xit5kDyaSFRkv5J1ek/cFxEh7sQZdsl6Wqf5QfcQ8ZfgnWTEipDvYv6DSdS464PM H7Kv/o5N9ZBD6AmKRIoPGhZ+6QZREKGF9aM9/Hk/vF2UM/L1BJvWEB0USCeh6ttCu3V9 UEL5IqTeNyQYu0cN6S4Epumw/k7ADah/J1MnaKqDfxxyKFT802iAiz+JdyyMRLfuANYv SyXYnbambk43WpEb69gOW2SkorRNp3KgW4PSFY3ac4mTp0lsfmm9U8n1xO2bhxC3yaPx 6dgw== X-Gm-Message-State: AMCzsaWMjAQtEQwRpxEu2MT9zckeNCgXBhe2US2zyQ1c2WzFydj+Lz72 iTVr3SCsIFLMathjW+bBjtrysFmhVNyBBaI3JbWlZntgaENW9FiHtBdN7VkmaYhEOQSXuPBLTqn UN2drIhkRNiDtySa70iPAL6kAscwg7rqiXhU= X-Google-Smtp-Source: ABhQp+ST6RAL3MJpaxxF5cvhwG/qqE+I0pcDLnja/g5bzY7hS+oCkntbXOVsR2FEjLuJiLppzs9i+b/s+PZP/oxmhsFsTw== MIME-Version: 1.0 X-Received: by 10.37.9.66 with SMTP id u2mr1810013ybm.83.1508454877513; Thu, 19 Oct 2017 16:14:37 -0700 (PDT) Date: Thu, 19 Oct 2017 16:14:32 -0700 Message-Id: <20171019231433.11723-1-mjg59@google.com> X-Mailer: git-send-email 2.15.0.rc0.271.g36b669edcc-goog Subject: [PATCH 1/2] security: Add a cred_getsecid hook From: Matthew Garrett To: linux-integrity@vger.kernel.org Cc: 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: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org 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 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 --- V2: incorporate Casey's requested change include/linux/lsm_hooks.h | 6 ++++++ include/linux/security.h | 1 + security/security.c | 7 +++++++ security/selinux/hooks.c | 8 ++++++++ security/smack/smack_lsm.c | 15 +++++++++++++++ 5 files changed, 37 insertions(+) diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index c9258124e417..c28c6f8b65dc 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. @@ -1507,6 +1511,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); @@ -1779,6 +1784,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 ce6265960d6c..14848fef8f62 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 4bf0f571b4ef..02d217597400 100644 --- a/security/security.c +++ b/security/security.c @@ -1004,6 +1004,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 f5d304736852..1d11679674a6 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3836,6 +3836,13 @@ 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) +{ + rcu_read_lock(); + *secid = cred_sid(c); + rcu_read_unlock(); +} + /* * set the security data for a kernel service * - all the creation contexts are set to unlabelled @@ -6338,6 +6345,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 286171a16ed2..ed1bbf201e2f 100644 --- a/security/smack/smack_lsm.c +++ b/security/smack/smack_lsm.c @@ -2049,6 +2049,20 @@ 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) +{ + rcu_read_lock(); + *secid = smk_of_task(c->security); + 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. @@ -4651,6 +4665,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),