Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5448937imm; Tue, 31 Jul 2018 11:03:34 -0700 (PDT) X-Google-Smtp-Source: AAOMgpd6XFflvOrkDKO2NKYOoQ838ER8dfxcm/RboxiQ0tXaJgN8GI28RuN24Nit3OsNRfTJNeHc X-Received: by 2002:a17:902:76c5:: with SMTP id j5-v6mr9181570plt.296.1533060214174; Tue, 31 Jul 2018 11:03:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533060214; cv=none; d=google.com; s=arc-20160816; b=YJ7G8tMH3dmsKW8qxKHCEw+GmewEGoWxnjCXHUgneEHGkdwJfyd5sN57SzQKNNK7eD AesUqYGkpSDOiPI9St9Hl9E5OsPNoR9cODu9Bw04hHmnlHL5L8x/wBokfgGuGGqSYqx/ pG6u2TThhw3j0XzpLj7X7TLWTSfk7JyHQ3PNUueDuOu4tV+3aQb0DL0Pd1iSLVA4gZj/ fqUpCKb/u8Ab5F+ztD3cqLxrYMYgwBiYoQBjgCQVcFdlvEZkmjmndFDjwXV4FSZrtHMx FAodtktOM1hg/TA313g7ytoSTeCkCJhhSL/Cdiu7dJNt45d4GgAlfggDtCWyn1JuAcvZ jBiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language:in-reply-to:mime-version :user-agent:date:message-id:from:references:to:subject :dkim-signature:arc-authentication-results; bh=NHiedC8fY7iXvZ81thRjkhciyIb74uCuJH4PAG6fZmg=; b=vBx0kOU6Hp8SIsaktAZPjtKM+oaygF6r4D66OXyf2Txmg+tBZdg7HptlkmIDuH8Oaa eC/a5XrY2RHTfk7NZSUk4wLCzEQTQuPFY7RKx96BufwcCtsiqMhLLk5SJ6vhSg5vJa/z mVXTARMdR/erDSKzrNURXDLM5NoiMs+6dnNsziKb/FNRMLkIdPOgpaikhnR5jtPfhAqS OunOuzQxXZ0RMoZG3EickOD743CEt0tsjoscuNOVNWBpKIG9Xevm9/Z0J9fDJD8Bq4rS lZgB15/M4il3JMCh0oM+8WKUObPVVsKt6pE+FQKSazrcw0KPpeEZHBbFWB4hu025toaG zj4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yahoo.com header.s=s2048 header.b=ReXY1eMS; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o8-v6si12388407pll.193.2018.07.31.11.03.14; Tue, 31 Jul 2018 11:03:34 -0700 (PDT) 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; dkim=pass header.i=@yahoo.com header.s=s2048 header.b=ReXY1eMS; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729782AbeGaTni (ORCPT + 99 others); Tue, 31 Jul 2018 15:43:38 -0400 Received: from sonic311-20.consmr.mail.bf2.yahoo.com ([74.6.131.194]:43123 "EHLO sonic311-20.consmr.mail.bf2.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729323AbeGaTnh (ORCPT ); Tue, 31 Jul 2018 15:43:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1533060128; bh=NHiedC8fY7iXvZ81thRjkhciyIb74uCuJH4PAG6fZmg=; h=Subject:To:References:From:Date:In-Reply-To:From:Subject; b=ReXY1eMSQLxldhbv3vS9zeI4DGOUvVhFEXunb4lKHu6yDptwjUhHUmy3PE41VUvJMtO3V4ch7trSEZ/si+IF52eE3ExRpSA56DEeR60FtjiWQw36wErox2bxhKs8m8H+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c6wkvFrJ.ZuxVZRWyu6.RsGtiCFEl4afvY17F4w9uPtxTPaCiC3dpzPM nvw2EAe0wTmwn5mwaVwrBX2UVhgCuGCjbPzDVlKsxTAhyFJwJGuXtuBy0u607wdHLAenr0dTYvmx 5qIEDotILazY_PuELlWJED6siO0gDVAqFCJK_18QTMWU4K_RXjiDjSqLiPVkJcLNNDTl6719fJVP SWkSrjIJ5LCtBOnSLIcox Received: from sonic.gate.mail.ne1.yahoo.com by sonic311.consmr.mail.bf2.yahoo.com with HTTP; Tue, 31 Jul 2018 18:02:08 +0000 Received: from c-67-169-65-224.hsd1.ca.comcast.net (EHLO [192.168.0.104]) ([67.169.65.224]) by smtp421.mail.bf1.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID 76b71a285760340c7cf8e276f7d88f71; Tue, 31 Jul 2018 18:02:06 +0000 (UTC) Subject: Re: [PATCH v2] security: Add LSM fixup hooks to set*gid syscalls. To: Micah Morton , linux-security-module@vger.kernel.org, serge@hallyn.com, Kees Cook , linux-kernel@vger.kernel.org References: From: Casey Schaufler Message-ID: <50f294e3-7d16-cc7b-e552-9001a65747e7@schaufler-ca.com> Date: Tue, 31 Jul 2018 11:02:00 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------DCEF4F4FFE3FC4DABDA1A570" Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a multi-part message in MIME format. --------------DCEF4F4FFE3FC4DABDA1A570 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit On 7/31/2018 10:34 AM, Micah Morton wrote: > 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 > What security module is going to provide a hook for this? > --- > 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. > > 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) >  { >  return call_int_hook(task_setpgid, 0, p, pgid); > > -- > 4.18-rc2 > > -- > --------------DCEF4F4FFE3FC4DABDA1A570 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 7/31/2018 10:34 AM, Micah Morton wrote:
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 <mortonm@chromium.org>
Acked-by: Kees Cook <keescook@chromium.org>
What security module is going to provide a hook for this?

---
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.

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)
 {
  return call_int_hook(task_setpgid, 0, p, pgid);

--
4.18-rc2

--


--------------DCEF4F4FFE3FC4DABDA1A570--