From patchwork Tue Jul 31 17:34:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Micah Morton X-Patchwork-Id: 10551119 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 08BEB13BB for ; Tue, 31 Jul 2018 17:35:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD47D2A260 for ; Tue, 31 Jul 2018 17:35:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CEE442A659; Tue, 31 Jul 2018 17:35:19 +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_SIGNED, HTML_MESSAGE,RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_TVD_MIME_EPI 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 D9EE12A260 for ; Tue, 31 Jul 2018 17:35:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729935AbeGaTQe (ORCPT ); Tue, 31 Jul 2018 15:16:34 -0400 Received: from mail-yw0-f194.google.com ([209.85.161.194]:40204 "EHLO mail-yw0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728607AbeGaTQe (ORCPT ); Tue, 31 Jul 2018 15:16:34 -0400 Received: by mail-yw0-f194.google.com with SMTP id z143-v6so6089803ywa.7 for ; Tue, 31 Jul 2018 10:35:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:from:date:message-id:subject:to; bh=JinvuXl4hZRvmQ4MiCXH09fuVomBkiUvNuwtC34RSY0=; b=OQkWlpx8tn9uXBdaZhPGeEE2LdAuTQycMK1QopT/XmOSs59opkIDeKMC4Ol9dzG5Sd CT+Koat0MIgmzWG5ve/gK+vg+Q8xAqKzHSj92VOibKZZfOii4Sy2zcsbKwaSvz5CTsoe cCHguy/TqPwBupCOGxbufntHFvGLIbQ+OKpKE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=JinvuXl4hZRvmQ4MiCXH09fuVomBkiUvNuwtC34RSY0=; b=fVp7oAgFdiVlR2w1iaEboYZ5mIfd6Olhwk6t6WjyfyG9sqdLbPrMrZ58LlcMTkZS6q hn1TtK8xMsPfqR2ZWKVEYFOk+PWHd/wBqJaa/Yw8QPcjWUonffqzGB3CJ93rhCyqb6UO oKEFFn669pIJL+pM2V2yfRddfugsRcWeUSOWylS2Mdy5Lmne8CpdrX6MSACB/z9XC7if hj5KTSiHGsq7ZfRdrU7Z4KINgAMnSBH389ol54uoGKCKM5r3Lg90zIUWsBRpDyJAoxjP FV08C3hMm0GRXamQo1tuNPWEnpHSTvXHO09kMh55I9L2A/wW22jx+0pX3iXMYbb14rxH Wxgg== X-Gm-Message-State: AOUpUlHqIk7wpzaErd8rWBxXPszSCVOJFHB/eyAIzPiZz+5b2Npqez1l jRYklWQTisuXONEU04NsRMmK/ao8Pgs6BX7anVCZnrpg//ZbsQ== X-Google-Smtp-Source: AAOMgpeg4q545HwQ/z/5kEl75engMEkgbW0a0UXaz1WegwQ97ancStrP39Ro/g3kENiCYCO6jQqO5WZhkcoLwDd5/A4= X-Received: by 2002:a0d:c3c5:: with SMTP id f188-v6mr11366699ywd.328.1533058510833; Tue, 31 Jul 2018 10:35:10 -0700 (PDT) MIME-Version: 1.0 From: Micah Morton Date: Tue, 31 Jul 2018 10:34:59 -0700 Message-ID: Subject: [PATCH v2] security: Add LSM fixup hooks to set*gid syscalls. To: linux-security-module@vger.kernel.org, serge@hallyn.com, Kees Cook , linux-kernel@vger.kernel.org Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The set*uid system calls all call an LSM fixup hook called security_task_fix_setuid, which allows for altering the behavior of those calls by a security module. Comments explaining the LSM_SETID_* constants in /include/linux/security.h imply that the constants are to be used for both the set*uid and set*gid syscalls, but the set*gid syscalls do not have the relevant hooks, meaning a security module can only alter syscalls that change user identity attributes but not ones that change group identity attributes. This patch adds the necessary LSM hook, called security_task_fix_setgid, and calls the hook from the appropriate places in the set*gid syscalls.Tested by putting a print statement in the hook and seeing it triggered from the various set*gid syscalls. Signed-off-by: Micah Morton Acked-by: Kees Cook Signed-off-by: Micah Morton Acked-by: Kees Cook --- NOTE: the security_task_fix_setgid line in sys_setfsgid is over 80 characters, but I figured I'd just follow how it was done in sys_setfsuid rather than trying to wrap the line, since the functions are nearly identical. return call_int_hook(task_setpgid, 0, p, pgid); -- 4.18-rc2 -- diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index 8f1131c8dd54..a2166c812a97 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -599,6 +599,15 @@ * @old is the set of credentials that are being replaces * @flags contains one of the LSM_SETID_* values. * Return 0 on success. + * @task_fix_setgid: + * Update the module's state after setting one or more of the group + * identity attributes of the current process. The @flags parameter + * indicates which of the set*gid system calls invoked this hook. + * @new is the set of credentials that will be installed. Modifications + * should be made to this rather than to @current->cred. + * @old is the set of credentials that are being replaced + * @flags contains one of the LSM_SETID_* values. + * Return 0 on success. * @task_setpgid: * Check permission before setting the process group identifier of the * process @p to @pgid. @@ -1587,6 +1596,8 @@ union security_list_options { enum kernel_read_file_id id); int (*task_fix_setuid)(struct cred *new, const struct cred *old, int flags); + int (*task_fix_setgid)(struct cred *new, const struct cred *old, + int flags); int (*task_setpgid)(struct task_struct *p, pid_t pgid); int (*task_getpgid)(struct task_struct *p); int (*task_getsid)(struct task_struct *p); @@ -1876,6 +1887,7 @@ struct security_hook_heads { struct hlist_head kernel_post_read_file; struct hlist_head kernel_module_request; struct hlist_head task_fix_setuid; + struct hlist_head task_fix_setgid; struct hlist_head task_setpgid; struct hlist_head task_getpgid; struct hlist_head task_getsid; diff --git a/include/linux/security.h b/include/linux/security.h index 63030c85ee19..a82d97cf13ab 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -325,6 +325,8 @@ int security_kernel_post_read_file(struct file *file, char *buf, loff_t size, enum kernel_read_file_id id); int security_task_fix_setuid(struct cred *new, const struct cred *old, int flags); +int security_task_fix_setgid(struct cred *new, const struct cred *old, + int flags); int security_task_setpgid(struct task_struct *p, pid_t pgid); int security_task_getpgid(struct task_struct *p); int security_task_getsid(struct task_struct *p); @@ -929,6 +931,13 @@ static inline int security_task_fix_setuid(struct cred *new, return cap_task_fix_setuid(new, old, flags); } +static inline int security_task_fix_setgid(struct cred *new, + const struct cred *old, + int flags) +{ + return 0; +} + static inline int security_task_setpgid(struct task_struct *p, pid_t pgid) { return 0; diff --git a/kernel/sys.c b/kernel/sys.c index 38509dc1f77b..f6ef922c6815 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -392,6 +392,10 @@ long __sys_setregid(gid_t rgid, gid_t egid) new->sgid = new->egid; new->fsgid = new->egid; + retval = security_task_fix_setgid(new, old, LSM_SETID_RE); + if (retval < 0) + goto error; + return commit_creds(new); error: @@ -434,6 +438,10 @@ long __sys_setgid(gid_t gid) else goto error; + retval = security_task_fix_setgid(new, old, LSM_SETID_ID); + if (retval < 0) + goto error; + return commit_creds(new); error: @@ -755,6 +763,10 @@ long __sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid) new->sgid = ksgid; new->fsgid = new->egid; + retval = security_task_fix_setgid(new, old, LSM_SETID_RES); + if (retval < 0) + goto error; + return commit_creds(new); error: @@ -861,7 +873,8 @@ long __sys_setfsgid(gid_t gid) ns_capable(old->user_ns, CAP_SETGID)) { if (!gid_eq(kgid, old->fsgid)) { new->fsgid = kgid; - goto change_okay; + if (security_task_fix_setgid(new, old, LSM_SETID_FS) == 0) + goto change_okay; } } diff --git a/security/security.c b/security/security.c index 68f46d849abe..587786fc0aaa 100644 --- a/security/security.c +++ b/security/security.c @@ -1062,6 +1062,12 @@ int security_task_fix_setuid(struct cred *new, const struct cred *old, return call_int_hook(task_fix_setuid, 0, new, old, flags); } +int security_task_fix_setgid(struct cred *new, const struct cred *old, + int flags) +{ + return call_int_hook(task_fix_setgid, 0, new, old, flags); +} + int security_task_setpgid(struct task_struct *p, pid_t pgid) {