smack: namespace groundwork
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 5 lines
(quoted diff) +4
-0+static inline void smack_userns_free(struct user_namespace *ns) +{ + struct smack_ns *snsp = ns->security; + struct smack_known *skp;
Review
Quoted 10 lines
(quoted diff) +10
-0+ struct smack_known_ns *sknp, *n; + + list_for_each_entry_safe(sknp, n, &snsp->smk_mapped, smk_list_ns) { + skp = sknp->smk_unmapped; + + mutex_lock(&skp->smk_mapped_lock); + list_del_rcu(&sknp->smk_list_known); + if (sknp->smk_allocated) + kfree(sknp->smk_mapped); + kfree(sknp);
sknp is released,
Review
Quoted 3 lines
(quoted diff) +3
-0+ mutex_unlock(&skp->smk_mapped_lock); + + list_del(&sknp->smk_list_ns);
but it is used again! typo?
Quoted 6 lines
(quoted diff) +5
-0+ } + + kfree(snsp); +} +
Quoted 18 lines · Hillf Danton
Quoted 9 lines
(quoted diff) +7
-0+static inline void smack_userns_free(struct user_namespace *ns) +{ + struct smack_ns *snsp = ns->security; + struct smack_known *skp; + struct smack_known_ns *sknp, *n; + + list_for_each_entry_safe(sknp, n, &snsp->smk_mapped,
smk_list_ns) {Review
Quoted 7 lines
(quoted diff) +7
-0+ skp = sknp->smk_unmapped; + + mutex_lock(&skp->smk_mapped_lock); + list_del_rcu(&sknp->smk_list_known); + if (sknp->smk_allocated) + kfree(sknp->smk_mapped); + kfree(sknp);sknp is released,
Quoted 5 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ mutex_unlock(&skp->smk_mapped_lock); + + list_del(&sknp->smk_list_ns);but it is used again! typo?
No, not a typo. A regular bug. Thanks for spotting it. Also sync mechanism before freeing was missing:
skp = sknp->smk_unmapped; mutex_lock(&skp->smk_mapped_lock); list_del_rcu(&sknp->smk_list_known); mutex_unlock(&skp->smk_mapped_lock); list_del(&sknp->smk_list_ns); call_rcu(&sknp->smk_rcu, smk_free_known_ns);
Signature · 3 lines
Lukasz Pawelczyk Samsung R&D Institute Poland Samsung Electronics
Quoted 2 lines · Lukasz Pawelczyk
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:
security/smack/smack.h +1 -0
@@ -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;
security/smack/smack_lsm.c +11 -3
@@ -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); + +call_rcu (&sknp->smk_rcu, smk_free_known_ns); }kfree (snsp);
Signature · 4 lines
-- Lukasz Pawelczyk Samsung R&D Institute Poland Samsung Electronics
Quoted 41 lines · Lukasz Pawelczyk
Quoted 3 lines
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:
security/smack/smack.h +1
-0@@ -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;security/smack/smack_lsm.c +9
-0@@ -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;
security/smack/smack_lsm.c static inline void smack_userns_free(struct user_namespace *ns) · line 4694
Quoted 9 lines
security/smack/smack_lsm.c +0
-3@@ -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)?
security/smack/smack_lsm.c static inline void smack_userns_free(struct user_namespace *ns)
Quoted 7 lines
security/smack/smack_lsm.c +2
-1+ +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;
+}
Quoted 8 lines
--
Lukasz Pawelczyk
Samsung R&D Institute Poland
Samsung Electronics
Quoted 22 lines · Hillf Danton
Quoted 5 lines
security/smack/smack_lsm.c +1
-0+static inline void smack_userns_free (struct user_namespace *ns) {struct smack_ns *snsp = ns->security;security/smack/smack_lsm.c static inline void smack_userns_free(struct · line 4695
Quoted 10 lines
security/smack/smack_lsm.c +0
-3@@ -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_delsafe, given the operation(quoted diff) +3
-0+ mutex_lock(&snsp->smk_mapped_lock); + list_add_rcu(&sknp->smk_list_ns, &snsp->smk_mapped); + mutex_unlock(&snsp->smk_mapped_lock);
Quoted 1 line
in
smk_import_mapped()function(copied below)?
Yes, the namespace is destroyed when all its references are gone. This also includes processes that were in that namespace. Meaning there is no way to import a new mapping for them anymore at this point.
Signature · 3 lines
Lukasz Pawelczyk Samsung R&D Institute Poland Samsung Electronics