All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Hillf Danton" <hillf.zj@alibaba-inc.com>
To: "'Lukasz Pawelczyk'" <l.pawelczyk@samsung.com>
Cc: "'Andy Lutomirski'" <luto@amacapital.net>,
	"'Kees Cook'" <keescook@chromium.org>,
	"'linux-kernel'" <linux-kernel@vger.kernel.org>,
	<linux-security-module@vger.kernel.org>
Subject: Re: [PATCH v4 09/11] smack: namespace groundwork
Date: Fri, 16 Oct 2015 11:04:54 +0800	[thread overview]
Message-ID: <026701d107bf$6ef3e280$4cdba780$@alibaba-inc.com> (raw)
In-Reply-To: <1444913602.5661.9.camel@samsung.com>

> On czw, 2015-10-15 at 14:41 +0200, Lukasz Pawelczyk wrote:
> 
> > No, not a typo. A regular bug. Thanks for spotting it. Also sync
> > mechanism before freeing was missing:
> 
> 
> Hitfix, will be integrated with the next respin:
> 
> diff --git a/security/smack/smack.h b/security/smack/smack.h
> index 3d432f4..3a795bf 100644
> --- a/security/smack/smack.h
> +++ b/security/smack/smack.h
> @@ -97,6 +97,7 @@ struct smack_ns {
>  struct smack_known_ns {
>         struct list_head        smk_list_known;
>         struct list_head        smk_list_ns;
> +       struct rcu_head         smk_rcu;
>         struct user_namespace   *smk_ns;
>         char                    *smk_mapped;
>         struct smack_known      *smk_unmapped;
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 8e0da67..234da71 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -4669,6 +4669,15 @@ static inline int smack_userns_create(struct user_namespace *ns)
>         return 0;
>  }
> 
> +static void smk_free_known_ns(struct rcu_head *head)
> +{
> +       struct smack_known_ns *sknp = container_of(head, struct smack_known_ns, smk_rcu);
> +
> +       if (sknp->smk_allocated)
> +               kfree(sknp->smk_mapped);
> +       kfree(sknp);
> +}
> +
>  static inline void smack_userns_free(struct user_namespace *ns)
>  {
>         struct smack_ns *snsp = ns->security;
> @@ -4680,12 +4689,11 @@ static inline void smack_userns_free(struct user_namespace *ns)
> 
>                 mutex_lock(&skp->smk_mapped_lock);
>                 list_del_rcu(&sknp->smk_list_known);
> -               if (sknp->smk_allocated)
> -                       kfree(sknp->smk_mapped);
> -               kfree(sknp);
>                 mutex_unlock(&skp->smk_mapped_lock);
> 
>                 list_del(&sknp->smk_list_ns);

Is list_del safe, given the operation

+	mutex_lock(&snsp->smk_mapped_lock);
+	list_add_rcu(&sknp->smk_list_ns, &snsp->smk_mapped);
+	mutex_unlock(&snsp->smk_mapped_lock);

in smk_import_mapped() function(copied below)?

> +
> +               call_rcu(&sknp->smk_rcu, smk_free_known_ns);
>         }
> 
>         kfree(snsp);
> --
>

+static struct smack_known_ns *smk_import_mapped(struct smack_known *skp,
+						struct user_namespace *ns,
+						const char *string, int len)
+{
+	struct smack_ns *snsp = ns->security;
+	struct smack_known_ns *sknp;
+	char *mapped;
+	bool allocated;
+
+	/* Mapping init_user_ns is against the design and pointless */
+	if (ns == &init_user_ns)
+		return ERR_PTR(-EBADR);
+
+	mapped = smk_parse_smack(string, len, &allocated);
+	if (IS_ERR(mapped))
+		return ERR_CAST(mapped);
+
+	mutex_lock(&skp->smk_mapped_lock);
+
+	/*
+	 * Don't allow one<->many mappings in namespace, rename.
+	 * This code won't get triggered for now as trying to assign
+	 * a duplicate is forbidden in proc_label_map_write().
+	 * Leaving this as this function might be also used elsewhere.
+	 */
+	sknp = smk_find_mapped(skp, ns);
+	if (sknp != NULL) {
+		if (sknp->smk_allocated)
+			kfree(sknp->smk_mapped);
+		sknp->smk_mapped = mapped;
+		sknp->smk_allocated = allocated;
+		goto unlockout;
+	}
+
+	sknp = kzalloc(sizeof(*sknp), GFP_KERNEL);
+	if (sknp == NULL) {
+		sknp = ERR_PTR(-ENOMEM);
+		if (allocated)
+			kfree(mapped);
+		goto unlockout;
+	}
+
+	sknp->smk_ns = ns;
+	sknp->smk_mapped = mapped;
+	sknp->smk_allocated = allocated;
+	sknp->smk_unmapped = skp;
+	list_add_rcu(&sknp->smk_list_known, &skp->smk_mapped);
+
+	mutex_lock(&snsp->smk_mapped_lock);
+	list_add_rcu(&sknp->smk_list_ns, &snsp->smk_mapped);
+	mutex_unlock(&snsp->smk_mapped_lock);
+
+unlockout:
+	mutex_unlock(&skp->smk_mapped_lock);
+
+	return sknp;
+}
 
> 
> 
> --
> Lukasz Pawelczyk
> Samsung R&D Institute Poland
> Samsung Electronics
> 
> 



  reply	other threads:[~2015-10-16  3:11 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <019801d1071b$2c124000$8436c000$@alibaba-inc.com>
2015-10-15  7:38 ` [PATCH v4 09/11] smack: namespace groundwork Hillf Danton
2015-10-15 12:41   ` Lukasz Pawelczyk
2015-10-15 12:53     ` Lukasz Pawelczyk
2015-10-16  3:04       ` Hillf Danton [this message]
2015-10-16 10:13         ` Lukasz Pawelczyk
2015-10-14 12:41 [PATCH v4 00/11] Smack namespace Lukasz Pawelczyk
     [not found] ` <1444826525-9758-1-git-send-email-l.pawelczyk-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org>
2015-10-14 12:42   ` [PATCH v4 09/11] smack: namespace groundwork Lukasz Pawelczyk
2015-10-14 12:42     ` Lukasz Pawelczyk
     [not found]     ` <1444826525-9758-10-git-send-email-l.pawelczyk-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org>
2015-10-29 22:51       ` Casey Schaufler
2015-10-29 22:51     ` Casey Schaufler
2015-10-29 22:51     ` Casey Schaufler
2015-10-14 12:42 ` Lukasz Pawelczyk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='026701d107bf$6ef3e280$4cdba780$@alibaba-inc.com' \
    --to=hillf.zj@alibaba-inc.com \
    --cc=keescook@chromium.org \
    --cc=l.pawelczyk@samsung.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@amacapital.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.