Skip to contentSkip to conversation

seccomp: don't leave dangling ->notif if file allocation fails

PATCH Applied Tycho Andersen 3 participants 3 messages Sep 2, 2020 – Sep 8, 2020 Secure computing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 seccomp: don't leave dangling ->notif if file allocation fails

Christian and Kees both pointed out that this is a bit sloppy to open-code both places, and Christian points out that we leave a dangling pointer to ->notif if file allocation fails. Since we check ->notif for null in order to determine if it's ok to install a filter, this means people won't be able to install a filter if the file allocation fails for some reason, even if they subsequently should be able to.

To fix this, let's hoist this free+null into its own little helper and use it.

---
 kernel/seccomp.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/seccomp.c +8 -3
index bb0dd9ae699a..676d4af62103 100644
@@ -1109,6 +1109,12 @@ static long seccomp_set_mode_strict(void)
 }
 
 #ifdef CONFIG_SECCOMP_FILTER
+static void seccomp_notify_free(struct seccomp_filter *filter)
+{
+	kfree(filter->notif);
+	filter->notif = NULL;
+}
+
 static void seccomp_notify_detach(struct seccomp_filter *filter)
 {
 	struct seccomp_knotif *knotif;
@@ -1138,8 +1144,7 @@ static void seccomp_notify_detach(struct seccomp_filter *filter)
 		complete(&knotif->ready);
 	}
 
-	kfree(filter->notif);
-	filter->notif = NULL;
+	seccomp_notify_free(filter);
 	mutex_unlock(&filter->notify_lock);
 }
 
@@ -1494,7 +1499,7 @@ static struct file *init_listener(struct seccomp_filter *filter)
 
 out_notif:
 	if (IS_ERR(ret))
-		kfree(filter->notif);
+		seccomp_notify_free(filter);
 out:
 	return ret;
 }

base-commit: 7b6aa0bb62fd6fd50f2d14136136262d28fb2dfe

Signature · 1 line
2.25.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Tycho Andersen

Christian and Kees both pointed out that this is a bit sloppy to open-code both places, and Christian points out that we leave a dangling pointer to ->notif if file allocation fails. Since we check ->notif for null in order to determine if it's ok to install a filter, this means people won't be able to install a filter if the file allocation fails for some reason, even if they subsequently should be able to.

Quoted 7 lines

To fix this, let's hoist this free+null into its own little helper and use it.

---

Thanks for the patch, Tycho! Looks simple enough to me:

kernel/seccomp.c static long seccomp_set_mode_strict(void) · line 1112

Quoted 12 lines
 kernel/seccomp.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/seccomp.c +1 -0
index bb0dd9ae699a..676d4af62103 100644
@@ -1109,6 +1109,12 @@ static long seccomp_set_mode_strict(void)
 }
 
 #ifdef CONFIG_SECCOMP_FILTER
+static void seccomp_notify_free(struct seccomp_filter *filter)

I think an explicit "inline" coldn't hurt but it's fine either way and no need to resend imho. Probably Kees can just add this when applying if we reall cared.

Quoted 32 lines
kernel/seccomp.c +7 -3
+{
+	kfree(filter->notif);
+	filter->notif = NULL;
+}
+
 static void seccomp_notify_detach(struct seccomp_filter *filter)
 {
 	struct seccomp_knotif *knotif;
@@ -1138,8 +1144,7 @@ static void seccomp_notify_detach(struct seccomp_filter *filter)
 		complete(&knotif->ready);
 	}
 
-	kfree(filter->notif);
-	filter->notif = NULL;
+	seccomp_notify_free(filter);
 	mutex_unlock(&filter->notify_lock);
 }
 
@@ -1494,7 +1499,7 @@ static struct file *init_listener(struct seccomp_filter *filter)
 
 out_notif:
 	if (IS_ERR(ret))
-		kfree(filter->notif);
+		seccomp_notify_free(filter);
 out:
 	return ret;
 }

base-commit: 7b6aa0bb62fd6fd50f2d14136136262d28fb2dfe

Signature · 1 line
2.25.1
Applied
#2 lore raw
In reply to Tycho Andersen
Quoted 8 lines · Tycho Andersen

Christian and Kees both pointed out that this is a bit sloppy to open-code both places, and Christian points out that we leave a dangling pointer to ->notif if file allocation fails. Since we check ->notif for null in order to determine if it's ok to install a filter, this means people won't be able to install a filter if the file allocation fails for some reason, even if they subsequently should be able to.

[...]

Applied, thanks!

[1/1] seccomp: don't leave dangling ->notif if file allocation fails

      https://git.kernel.org/kees/c/e839317900e9

as featured on my Twitch stream! ;)

Signature · 1 line
Kees Cook