Re: [PATCH 07/32] selinux: Implement the new mount API LSM hooks [ver #9]

2018-07-11 Thread Stephen Smalley
On 07/10/2018 06:42 PM, David Howells wrote:
> Implement the new mount API LSM hooks for SELinux.  At some point the old
> hooks will need to be removed.
> 
> Question: Should the ->fs_context_parse_source() hook be implemented to
> check the labels on any source devices specified?

The hook interface doesn't appear to lend itself to such validation, since you 
are just passing a string, not an inode.
Looking up the inode within the security module could easily yield a different 
object than what is ultimately used for the actual mount.

> 
> Signed-off-by: David Howells 
> cc: Paul Moore 
> cc: Stephen Smalley 
> cc: selinux@tycho.nsa.gov
> cc: linux-security-mod...@vger.kernel.org
> ---
> 
>  security/selinux/hooks.c |  264 
> ++
>  1 file changed, 264 insertions(+)
> 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 5bb53edd74cc..bdecae4b7306 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -48,6 +48,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -2973,6 +2974,261 @@ static int selinux_umount(struct vfsmount *mnt, int 
> flags)
>  FILESYSTEM__UNMOUNT, NULL);
>  }
>  
> +/* fsopen mount context operations */
> +
> +static int selinux_fs_context_alloc(struct fs_context *fc,
> + struct dentry *reference)
> +{
> + struct security_mnt_opts *opts;
> +
> + opts = kzalloc(sizeof(*opts), GFP_KERNEL);
> + if (!opts)
> + return -ENOMEM;
> +
> + fc->security = opts;
> + return 0;
> +}
> +
> +static int selinux_fs_context_dup(struct fs_context *fc,
> +   struct fs_context *src_fc)
> +{
> + const struct security_mnt_opts *src = src_fc->security;
> + struct security_mnt_opts *opts;
> + int i, n;
> +
> + opts = kzalloc(sizeof(*opts), GFP_KERNEL);
> + if (!opts)
> + return -ENOMEM;
> + fc->security = opts;
> +
> + if (!src || !src->num_mnt_opts)
> + return 0;
> + n = opts->num_mnt_opts = src->num_mnt_opts;
> +
> + if (src->mnt_opts) {
> + opts->mnt_opts = kcalloc(n, sizeof(char *), GFP_KERNEL);
> + if (!opts->mnt_opts)
> + return -ENOMEM;
> +
> + for (i = 0; i < n; i++) {
> + if (src->mnt_opts[i]) {
> + opts->mnt_opts[i] = kstrdup(src->mnt_opts[i],
> + GFP_KERNEL);
> + if (!opts->mnt_opts[i])
> + return -ENOMEM;
> + }
> + }
> + }
> +
> + if (src->mnt_opts_flags) {
> + opts->mnt_opts_flags = kmemdup(src->mnt_opts_flags,
> +n * sizeof(int), GFP_KERNEL);
> + if (!opts->mnt_opts_flags)
> + return -ENOMEM;
> + }
> +
> + return 0;
> +}
> +
> +static void selinux_fs_context_free(struct fs_context *fc)
> +{
> + struct security_mnt_opts *opts = fc->security;
> +
> + if (opts) {
> + security_free_mnt_opts(opts);
> + fc->security = NULL;
> + }
> +}
> +
> +static int selinux_fs_context_parse_option(struct fs_context *fc, char *opt, 
> size_t len)
> +{
> + struct security_mnt_opts *opts = fc->security;
> + substring_t args[MAX_OPT_ARGS];
> + unsigned int have;
> + char *c, **oo;
> + int token, ctx, i, *of;
> +
> + token = match_token(opt, tokens, args);
> + if (token == Opt_error)
> + return 0; /* Doesn't belong to us. */
> +
> + have = 0;
> + for (i = 0; i < opts->num_mnt_opts; i++)
> + have |= 1 << opts->mnt_opts_flags[i];
> + if (have & (1 << token))
> + return -EINVAL;
> +
> + switch (token) {
> + case Opt_context:
> + if (have & (1 << Opt_defcontext))
> + goto incompatible;
> + ctx = CONTEXT_MNT;
> + goto copy_context_string;
> +
> + case Opt_fscontext:
> + ctx = FSCONTEXT_MNT;
> + goto copy_context_string;
> +
> + case Opt_rootcontext:
> + ctx = ROOTCONTEXT_MNT;
> + goto copy_context_string;
> +
> + case Opt_defcontext:
> + if (have & (1 << Opt_context))
> + goto incompatible;
> + ctx = DEFCONTEXT_MNT;
> + goto copy_context_string;
> +
> + case Opt_labelsupport:
> + return 1;
> +
> + default:
> + return -EINVAL;
> + }
> +
> +copy_context_string:
> + if (opts->num_mnt_opts > 3)
> + return -EINVAL;
> +
> + of = krealloc(opts->mnt_opts_flags,
> +   (opts->num_mnt_opts + 1) * sizeof(int), GFP_KERNEL);
> + if (!of)
> + return -ENOMEM;
> + of[opts->num_mnt_opts] = 0;
> +  

[PATCH 07/32] selinux: Implement the new mount API LSM hooks [ver #9]

2018-07-10 Thread David Howells
Implement the new mount API LSM hooks for SELinux.  At some point the old
hooks will need to be removed.

Question: Should the ->fs_context_parse_source() hook be implemented to
check the labels on any source devices specified?

Signed-off-by: David Howells 
cc: Paul Moore 
cc: Stephen Smalley 
cc: selinux@tycho.nsa.gov
cc: linux-security-mod...@vger.kernel.org
---

 security/selinux/hooks.c |  264 ++
 1 file changed, 264 insertions(+)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 5bb53edd74cc..bdecae4b7306 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -48,6 +48,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -2973,6 +2974,261 @@ static int selinux_umount(struct vfsmount *mnt, int 
flags)
   FILESYSTEM__UNMOUNT, NULL);
 }
 
+/* fsopen mount context operations */
+
+static int selinux_fs_context_alloc(struct fs_context *fc,
+   struct dentry *reference)
+{
+   struct security_mnt_opts *opts;
+
+   opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+   if (!opts)
+   return -ENOMEM;
+
+   fc->security = opts;
+   return 0;
+}
+
+static int selinux_fs_context_dup(struct fs_context *fc,
+ struct fs_context *src_fc)
+{
+   const struct security_mnt_opts *src = src_fc->security;
+   struct security_mnt_opts *opts;
+   int i, n;
+
+   opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+   if (!opts)
+   return -ENOMEM;
+   fc->security = opts;
+
+   if (!src || !src->num_mnt_opts)
+   return 0;
+   n = opts->num_mnt_opts = src->num_mnt_opts;
+
+   if (src->mnt_opts) {
+   opts->mnt_opts = kcalloc(n, sizeof(char *), GFP_KERNEL);
+   if (!opts->mnt_opts)
+   return -ENOMEM;
+
+   for (i = 0; i < n; i++) {
+   if (src->mnt_opts[i]) {
+   opts->mnt_opts[i] = kstrdup(src->mnt_opts[i],
+   GFP_KERNEL);
+   if (!opts->mnt_opts[i])
+   return -ENOMEM;
+   }
+   }
+   }
+
+   if (src->mnt_opts_flags) {
+   opts->mnt_opts_flags = kmemdup(src->mnt_opts_flags,
+  n * sizeof(int), GFP_KERNEL);
+   if (!opts->mnt_opts_flags)
+   return -ENOMEM;
+   }
+
+   return 0;
+}
+
+static void selinux_fs_context_free(struct fs_context *fc)
+{
+   struct security_mnt_opts *opts = fc->security;
+
+   if (opts) {
+   security_free_mnt_opts(opts);
+   fc->security = NULL;
+   }
+}
+
+static int selinux_fs_context_parse_option(struct fs_context *fc, char *opt, 
size_t len)
+{
+   struct security_mnt_opts *opts = fc->security;
+   substring_t args[MAX_OPT_ARGS];
+   unsigned int have;
+   char *c, **oo;
+   int token, ctx, i, *of;
+
+   token = match_token(opt, tokens, args);
+   if (token == Opt_error)
+   return 0; /* Doesn't belong to us. */
+
+   have = 0;
+   for (i = 0; i < opts->num_mnt_opts; i++)
+   have |= 1 << opts->mnt_opts_flags[i];
+   if (have & (1 << token))
+   return -EINVAL;
+
+   switch (token) {
+   case Opt_context:
+   if (have & (1 << Opt_defcontext))
+   goto incompatible;
+   ctx = CONTEXT_MNT;
+   goto copy_context_string;
+
+   case Opt_fscontext:
+   ctx = FSCONTEXT_MNT;
+   goto copy_context_string;
+
+   case Opt_rootcontext:
+   ctx = ROOTCONTEXT_MNT;
+   goto copy_context_string;
+
+   case Opt_defcontext:
+   if (have & (1 << Opt_context))
+   goto incompatible;
+   ctx = DEFCONTEXT_MNT;
+   goto copy_context_string;
+
+   case Opt_labelsupport:
+   return 1;
+
+   default:
+   return -EINVAL;
+   }
+
+copy_context_string:
+   if (opts->num_mnt_opts > 3)
+   return -EINVAL;
+
+   of = krealloc(opts->mnt_opts_flags,
+ (opts->num_mnt_opts + 1) * sizeof(int), GFP_KERNEL);
+   if (!of)
+   return -ENOMEM;
+   of[opts->num_mnt_opts] = 0;
+   opts->mnt_opts_flags = of;
+
+   oo = krealloc(opts->mnt_opts,
+ (opts->num_mnt_opts + 1) * sizeof(char *), GFP_KERNEL);
+   if (!oo)
+   return -ENOMEM;
+   oo[opts->num_mnt_opts] = NULL;
+   opts->mnt_opts = oo;
+
+   c = match_strdup(&args[0]);
+   if (!c)
+   return -ENOMEM;
+   opts->mnt_opts[opts->num_mnt_opts] = c;
+   opts->mnt_opts_flags[opts->num_mnt_opts]