Skip to content

Commit 9f87dcf

Browse files
sargunkees
authored andcommitted
seccomp: Add find_notification helper
This adds a helper which can iterate through a seccomp_filter to find a notification matching an ID. It removes several replicated chunks of code. Signed-off-by: Sargun Dhillon <[email protected]> Acked-by: Christian Brauner <[email protected]> Reviewed-by: Tycho Andersen <[email protected]> Cc: Matt Denton <[email protected]> Cc: Kees Cook <[email protected]>, Cc: Jann Horn <[email protected]>, Cc: Robert Sesek <[email protected]>, Cc: Chris Palmer <[email protected]> Cc: Christian Brauner <[email protected]> Cc: Tycho Andersen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent c818c03 commit 9f87dcf

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

kernel/seccomp.c

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <linux/tracehook.h>
4242
#include <linux/uaccess.h>
4343
#include <linux/anon_inodes.h>
44+
#include <linux/lockdep.h>
4445

4546
enum notify_state {
4647
SECCOMP_NOTIFY_INIT,
@@ -1024,6 +1025,23 @@ static int seccomp_notify_release(struct inode *inode, struct file *file)
10241025
return 0;
10251026
}
10261027

1028+
/* must be called with notif_lock held */
1029+
static inline struct seccomp_knotif *
1030+
find_notification(struct seccomp_filter *filter, u64 id)
1031+
{
1032+
struct seccomp_knotif *cur;
1033+
1034+
lockdep_assert_held(&filter->notify_lock);
1035+
1036+
list_for_each_entry(cur, &filter->notif->notifications, list) {
1037+
if (cur->id == id)
1038+
return cur;
1039+
}
1040+
1041+
return NULL;
1042+
}
1043+
1044+
10271045
static long seccomp_notify_recv(struct seccomp_filter *filter,
10281046
void __user *buf)
10291047
{
@@ -1081,15 +1099,8 @@ static long seccomp_notify_recv(struct seccomp_filter *filter,
10811099
* may have died when we released the lock, so we need to make
10821100
* sure it's still around.
10831101
*/
1084-
knotif = NULL;
10851102
mutex_lock(&filter->notify_lock);
1086-
list_for_each_entry(cur, &filter->notif->notifications, list) {
1087-
if (cur->id == unotif.id) {
1088-
knotif = cur;
1089-
break;
1090-
}
1091-
}
1092-
1103+
knotif = find_notification(filter, unotif.id);
10931104
if (knotif) {
10941105
knotif->state = SECCOMP_NOTIFY_INIT;
10951106
up(&filter->notif->request);
@@ -1104,7 +1115,7 @@ static long seccomp_notify_send(struct seccomp_filter *filter,
11041115
void __user *buf)
11051116
{
11061117
struct seccomp_notif_resp resp = {};
1107-
struct seccomp_knotif *knotif = NULL, *cur;
1118+
struct seccomp_knotif *knotif;
11081119
long ret;
11091120

11101121
if (copy_from_user(&resp, buf, sizeof(resp)))
@@ -1121,13 +1132,7 @@ static long seccomp_notify_send(struct seccomp_filter *filter,
11211132
if (ret < 0)
11221133
return ret;
11231134

1124-
list_for_each_entry(cur, &filter->notif->notifications, list) {
1125-
if (cur->id == resp.id) {
1126-
knotif = cur;
1127-
break;
1128-
}
1129-
}
1130-
1135+
knotif = find_notification(filter, resp.id);
11311136
if (!knotif) {
11321137
ret = -ENOENT;
11331138
goto out;
@@ -1153,7 +1158,7 @@ static long seccomp_notify_send(struct seccomp_filter *filter,
11531158
static long seccomp_notify_id_valid(struct seccomp_filter *filter,
11541159
void __user *buf)
11551160
{
1156-
struct seccomp_knotif *knotif = NULL;
1161+
struct seccomp_knotif *knotif;
11571162
u64 id;
11581163
long ret;
11591164

@@ -1164,16 +1169,12 @@ static long seccomp_notify_id_valid(struct seccomp_filter *filter,
11641169
if (ret < 0)
11651170
return ret;
11661171

1167-
ret = -ENOENT;
1168-
list_for_each_entry(knotif, &filter->notif->notifications, list) {
1169-
if (knotif->id == id) {
1170-
if (knotif->state == SECCOMP_NOTIFY_SENT)
1171-
ret = 0;
1172-
goto out;
1173-
}
1174-
}
1172+
knotif = find_notification(filter, id);
1173+
if (knotif && knotif->state == SECCOMP_NOTIFY_SENT)
1174+
ret = 0;
1175+
else
1176+
ret = -ENOENT;
11751177

1176-
out:
11771178
mutex_unlock(&filter->notify_lock);
11781179
return ret;
11791180
}

0 commit comments

Comments
 (0)