Skip to content

Commit 805517e

Browse files
committed
wip: debug code cleanup, fix erroneous goto and other improvements
Signed-off-by: Sudipta Pandit <[email protected]>
1 parent 8e714fb commit 805517e

File tree

3 files changed

+124
-100
lines changed

3 files changed

+124
-100
lines changed

src/system.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,12 @@ int sys_notify_id_valid(int fd, uint64_t id)
585585
*/
586586
int sys_notify_addfd(int fd, struct seccomp_notif_addfd *addfd)
587587
{
588+
int rc;
588589
if (state.sup_user_notif <= 0)
589590
return -EOPNOTSUPP;
590591

591-
int rc = ioctl(fd, SECCOMP_IOCTL_NOTIF_ADDFD, addfd);
592-
if ( rc < 0 && errno == EINVAL)
592+
rc = ioctl(fd, SECCOMP_IOCTL_NOTIF_ADDFD, addfd);
593+
if (rc < 0 && errno == EINVAL)
593594
return -EOPNOTSUPP;
594595
if (rc < 0)
595596
return -ECANCELED;

tests/63-live-notify_addfd.c

Lines changed: 121 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <errno.h>
2323
#include <fcntl.h>
24+
#include <signal.h>
2425
#include <seccomp.h>
2526
#include <string.h>
2627
#include <sys/socket.h>
@@ -31,7 +32,7 @@
3132
int send_fd(int sock, int fd)
3233
{
3334
struct iovec iov = {.iov_base = "F", .iov_len = 1};
34-
char buffer[CMSG_SPACE(sizeof(fd))]; // Do i need to set it to zero?
35+
char buffer[CMSG_SPACE(sizeof(fd))];
3536
memset(buffer, 0, sizeof(buffer));
3637

3738
struct msghdr msg = {
@@ -73,14 +74,116 @@ int recv_fd(int sock)
7374
return fd;
7475
}
7576

76-
int main(int argc, char *argv[])
77+
void child_process(scmp_filter_ctx ctx, int sock_fd)
7778
{
78-
int rc, status;
79-
int sock_pair[2];
80-
int notify_fd = -1, new_fd = -1;
79+
int rc;
80+
int ret = -1;
81+
int notify_fd = -1;
82+
char buf[128];
83+
ssize_t bytes_read = -1;
84+
85+
rc = seccomp_load(ctx);
86+
if (rc < 0)
87+
goto out;
88+
89+
rc = seccomp_notify_fd(ctx);
90+
if (rc < 0)
91+
goto out;
92+
notify_fd = rc;
93+
94+
rc = send_fd(sock_fd, notify_fd);
95+
if (rc < 0) {
96+
rc = -errno;
97+
goto out;
98+
}
99+
100+
ret = openat(AT_FDCWD, "/etc/hostname", O_RDONLY);
101+
if (ret < 0) {
102+
rc = -errno;
103+
goto out;
104+
}
105+
106+
bytes_read = read(ret, buf, sizeof(buf));
107+
rc = bytes_read;
108+
109+
out:
110+
if (notify_fd >= 0)
111+
close(notify_fd);
112+
if (ret >= 0)
113+
close(ret);
114+
close(sock_fd);
115+
exit(rc);
116+
}
117+
118+
int parent_process(int sock_fd)
119+
{
120+
int rc;
121+
int notify_fd = -1;
122+
int new_fd = -1;
123+
int installed_fd = -1;
81124
struct seccomp_notif *req = NULL;
82125
struct seccomp_notif_resp *resp = NULL;
83126
struct seccomp_notif_addfd addfd = {0};
127+
128+
rc = recv_fd(sock_fd);
129+
if (rc < 0) {
130+
rc = -errno;
131+
goto out;
132+
}
133+
notify_fd = rc;
134+
135+
rc = seccomp_notify_alloc(&req, &resp);
136+
if (rc)
137+
goto out;
138+
139+
rc = seccomp_notify_receive(notify_fd, req);
140+
if (rc)
141+
goto out;
142+
if (req->data.nr != __NR_openat) {
143+
rc = -EFAULT;
144+
goto out;
145+
}
146+
147+
new_fd = openat(AT_FDCWD, "/dev/null", O_RDONLY);
148+
if (new_fd < 0) {
149+
rc = -errno;
150+
goto out;
151+
}
152+
153+
memset(&addfd, 0, sizeof(addfd));
154+
addfd.id = req->id;
155+
addfd.srcfd = new_fd;
156+
addfd.newfd = 0;
157+
addfd.flags = 0;
158+
rc = seccomp_notify_addfd(notify_fd, &addfd);
159+
if (rc < 0)
160+
goto out;
161+
installed_fd = rc;
162+
163+
rc = seccomp_notify_id_valid(notify_fd, req->id);
164+
if (rc)
165+
goto out;
166+
167+
resp->id = req->id;
168+
resp->val = installed_fd;
169+
resp->error = 0;
170+
resp->flags = 0;
171+
rc = seccomp_notify_respond(notify_fd, resp);
172+
173+
out:
174+
if (notify_fd >= 0)
175+
close(notify_fd);
176+
if (new_fd >= 0)
177+
close(new_fd);
178+
close(sock_fd);
179+
seccomp_notify_free(req, resp);
180+
return rc;
181+
}
182+
183+
int main(int argc, char *argv[])
184+
{
185+
int rc, status;
186+
int sock_pair[2];
84187
scmp_filter_ctx ctx = NULL;
85188
pid_t pid = 0;
86189

@@ -92,7 +195,7 @@ int main(int argc, char *argv[])
92195
if (rc)
93196
goto out;
94197

95-
// set up socket pair for sending notify_fd
198+
/* set up socket pair for sending notify_fd */
96199
rc = socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock_pair);
97200
if (rc < 0) {
98201
rc = -errno;
@@ -101,83 +204,11 @@ int main(int argc, char *argv[])
101204

102205
pid = fork();
103206
if (pid == 0) {
104-
close(sock_pair[0]); // close the parent's end
105-
106-
rc = seccomp_load(ctx);
107-
if (rc < 0)
108-
goto out;
109-
110-
rc = seccomp_notify_fd(ctx);
111-
if (rc < 0)
112-
goto out;
113-
notify_fd = rc;
114-
115-
rc = send_fd(sock_pair[1], notify_fd);
116-
if (rc < 0) {
117-
rc = -errno;
118-
goto out;
119-
}
120-
close(notify_fd);
121-
122-
int ret = openat(AT_FDCWD, "/etc/hostname", O_RDONLY);
123-
if (ret < 0) {
124-
exit(ret);
125-
}
126-
127-
char buf[128];
128-
ssize_t bytes_read = read(ret, buf, sizeof(buf));
129-
130-
close(ret);
131-
close(sock_pair[1]);
132-
exit(bytes_read); // bytes_read should be 0, as it's reading /dev/null
207+
close(sock_pair[0]); /* close the parent's end */
208+
child_process(ctx, sock_pair[1]);
133209
} else {
134-
close(sock_pair[1]); // close the child's end
135-
rc = recv_fd(sock_pair[0]);
136-
if (rc < 0) {
137-
rc = -errno;
138-
goto out;
139-
}
140-
notify_fd = rc;
141-
142-
rc = seccomp_notify_alloc(&req, &resp);
143-
if (rc)
144-
goto out;
145-
146-
rc = seccomp_notify_receive(notify_fd, req);
147-
if (rc)
148-
goto out;
149-
if (req->data.nr != __NR_openat) {
150-
rc = -EFAULT;
151-
goto out;
152-
}
153-
154-
new_fd = openat(AT_FDCWD, "/dev/null", O_RDONLY);
155-
if (new_fd < 0) {
156-
rc = -errno;
157-
goto out;
158-
}
159-
160-
memset(&addfd, 0, sizeof(addfd));
161-
addfd.id = req->id;
162-
addfd.srcfd = new_fd;
163-
addfd.newfd = 0;
164-
addfd.flags = 0;
165-
rc = seccomp_notify_addfd(notify_fd, &addfd);
166-
if (rc < 0)
167-
goto out;
168-
int installed_fd = rc;
169-
170-
rc = seccomp_notify_id_valid(notify_fd, req->id);
171-
if (rc)
172-
goto out;
173-
174-
resp->id = req->id;
175-
resp->val = installed_fd;
176-
resp->error = 0;
177-
resp->flags = 0;
178-
rc = seccomp_notify_respond(notify_fd, resp);
179-
if (rc)
180-
goto out;
210+
close(sock_pair[1]); /* close the child's end */
211+
rc = parent_process(sock_pair[0]);
181212

182213
if (waitpid(pid, &status, 0) != pid) {
183214
rc = -EFAULT;
@@ -192,19 +223,14 @@ int main(int argc, char *argv[])
192223
rc = -EFAULT;
193224
goto out;
194225
}
226+
}
195227

196228
out:
197-
if (notify_fd >= 0)
198-
close(notify_fd);
199-
if (new_fd >= 0)
200-
close(new_fd);
201-
if (pid)
202-
kill(pid, SIGKILL);
203-
seccomp_notify_free(req, resp);
204-
seccomp_release(ctx);
205-
206-
if (rc != 0)
207-
return (rc < 0 ? -rc : rc);
208-
return 160;
209-
}
229+
if (pid)
230+
kill(pid, SIGKILL);
231+
seccomp_release(ctx);
232+
233+
if (rc != 0)
234+
return (rc < 0 ? -rc : rc);
235+
return 160;
210236
}

tests/63-live-notify_addfd.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def test():
5959

6060
ret_fd = os.open("/etc/hostname", os.O_RDONLY)
6161
if ret_fd < 0:
62-
# raise RuntimeError("Response return value failed")
6362
quit(ret_fd)
6463

6564
ret_bytes = os.read(ret_fd, 128)
@@ -80,9 +79,7 @@ def test():
8079
raise RuntimeError("Notification failed")
8180

8281
new_fd = os.open("/dev/null", os.O_RDONLY)
83-
# print("New fd", new_fd)
8482
installed_fd = f.notify_addfd(NotificationAddfd(notify, 0, new_fd), fd=notify_fd)
85-
# print("Installed fd", installed_fd)
8683
f.respond_notify(NotificationResponse(notify, installed_fd, 0, 0), fd=notify_fd)
8784

8885
# No longer need the fds

0 commit comments

Comments
 (0)