Skip to content

Commit d5adf4a

Browse files
committed
api: add the seccomp notify addfd support
Add support for the seccomp notify addfd feature in the core API. It allows a caller to install fd in target's fd table. Signed-off-by: Sudipta Pandit <sudpandit@microsoft.com>
1 parent 5491c4b commit d5adf4a

6 files changed

Lines changed: 97 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.so man3/seccomp_notify_alloc.3

doc/man/man3/seccomp_notify_alloc.3

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.SH NAME
44
.\" //////////////////////////////////////////////////////////////////////////
55
seccomp_notify_alloc, seccomp_notify_free, seccomp_notify_receive,
6-
seccomp_notify_respond, seccomp_notify_id_valid, seccomp_notify_fd \- Manage seccomp notifications
6+
seccomp_notify_respond, seccomp_notify_id_valid, seccomp_notify_fd, seccomp_notify_addfd \- Manage seccomp notifications
77
.\" //////////////////////////////////////////////////////////////////////////
88
.SH SYNOPSIS
99
.\" //////////////////////////////////////////////////////////////////////////
@@ -16,6 +16,7 @@ seccomp_notify_respond, seccomp_notify_id_valid, seccomp_notify_fd \- Manage sec
1616
.BI "int seccomp_notify_respond(int " fd ", struct seccomp_notif_resp *" resp ")"
1717
.BI "int seccomp_notify_id_valid(int " fd ", uint64_t " id ")"
1818
.BI "int seccomp_notify_fd(const scmp_filter_ctx " ctx ")"
19+
.BI "int seccomp_notify_addfd(int " fd ", struct seccomp_notif_addfd *" addfd ")"
1920
.sp
2021
Link with \fI\-lseccomp\fP.
2122
.fi
@@ -54,6 +55,11 @@ race conditions.
5455
The
5556
.BR seccomp_notify_fd ()
5657
returns the notification fd of a filter after it has been loaded.
58+
.P
59+
The
60+
.BR seccomp_notify_addfd ()
61+
function enables the caller to install a file descriptor into the target's file descriptor table.
62+
The id field of the struct should be the same as the id from the request.
5763
.\" //////////////////////////////////////////////////////////////////////////
5864
.SH RETURN VALUE
5965
.\" //////////////////////////////////////////////////////////////////////////
@@ -67,6 +73,10 @@ The
6773
returns 0 if the id is valid, and -ENOENT if it is not.
6874
.P
6975
The
76+
.BR seccomp_notify_addfd ()
77+
returns the installed fd number on success, and one of error codes mentioned below on failure.
78+
.P
79+
The
7080
.BR seccomp_notify_alloc (),
7181
.BR seccomp_notify_receive (),
7282
and

include/seccomp.h.in

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,24 @@ struct seccomp_notif_resp {
405405
__s32 error;
406406
__u32 flags;
407407
};
408+
409+
#endif
410+
411+
/* seccomp_notif_addfd and ADDFD_FLAG_SETFD was added in kernel v5.10 */
412+
#ifndef SECCOMP_ADDFD_FLAG_SETFD
413+
#define SECCOMP_ADDFD_FLAG_SETFD (1UL << 0)
414+
struct seccomp_notif_addfd {
415+
__u64 id;
416+
__u32 flags;
417+
__u32 srcfd;
418+
__u32 newfd;
419+
__u32 newfd_flags;
420+
};
421+
#endif
422+
423+
/* Addfd and return it, atomically. ADDFD_FLAG_SEND was added in kernel 5.14 */
424+
#ifndef SECCOMP_ADDFD_FLAG_SEND
425+
#define SECCOMP_ADDFD_FLAG_SEND (1UL << 1)
408426
#endif
409427

410428
/*
@@ -814,6 +832,18 @@ int seccomp_notify_id_valid(int fd, uint64_t id);
814832
*/
815833
int seccomp_notify_fd(const scmp_filter_ctx ctx);
816834

835+
/**
836+
* Install a file descriptor into the target's process.
837+
* @param fd the notification fd
838+
* @param addfd the addfd structure
839+
*
840+
* This function enables the caller to install/add a fd into the
841+
* target's fd table. Returns the installed fd number on success and,
842+
* negative values on failure.
843+
*
844+
*/
845+
int seccomp_notify_addfd(int fd, struct seccomp_notif_addfd *addfd);
846+
817847
/**
818848
* Generate seccomp Pseudo Filter Code (PFC) and export it to a file
819849
* @param ctx the filter context

src/api.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,15 @@ API int seccomp_notify_fd(const scmp_filter_ctx ctx)
728728
return _rc_filter(sys_notify_fd());
729729
}
730730

731+
/* NOTE - function header comment in include/seccomp.h */
732+
API int seccomp_notify_addfd(int fd, struct seccomp_notif_addfd *addfd)
733+
{
734+
/* force a runtime api level detection */
735+
_seccomp_api_update();
736+
737+
return _rc_filter(sys_notify_addfd(fd, addfd));
738+
}
739+
731740
/* NOTE - function header comment in include/seccomp.h */
732741
API int seccomp_export_pfc(const scmp_filter_ctx ctx, int fd)
733742
{

src/system.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,25 @@ int sys_notify_id_valid(int fd, uint64_t id)
573573
return -ENOENT;
574574
return 0;
575575
}
576+
577+
/**
578+
* Install a file descriptor into the target's process.
579+
* @param fd the notification fd
580+
* @param addfd the addfd structure
581+
*
582+
* Install a file descriptor into the target's process. Returns the
583+
* installed fd number on success, negative values on failure.
584+
*
585+
*/
586+
int sys_notify_addfd(int fd, struct seccomp_notif_addfd *addfd)
587+
{
588+
if (state.sup_user_notif <= 0)
589+
return -EOPNOTSUPP;
590+
591+
int rc = ioctl(fd, SECCOMP_IOCTL_NOTIF_ADDFD, addfd);
592+
if ( rc < 0 && errno == EINVAL)
593+
return -EOPNOTSUPP;
594+
if (rc < 0)
595+
return -ECANCELED;
596+
return rc;
597+
}

src/system.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,29 @@ struct seccomp_notif_resp {
193193
#define SECCOMP_IOCTL_NOTIF_ID_VALID SECCOMP_IOW(2, __u64)
194194
#endif /* SECCOMP_RET_USER_NOTIF */
195195

196+
/* seccomp_notif_addfd and ADDFD_FLAG_SETFD was added in kernel v5.10 */
197+
#ifndef SECCOMP_ADDFD_FLAG_SETFD
198+
#define SECCOMP_ADDFD_FLAG_SETFD (1UL << 0)
199+
struct seccomp_notif_addfd {
200+
__u64 id;
201+
__u32 flags;
202+
__u32 srcfd;
203+
__u32 newfd;
204+
__u32 newfd_flags;
205+
};
206+
#endif
207+
208+
/* Addfd and return it, atomically. ADDFD_FLAG_SEND was added in kernel 5.14 */
209+
#ifndef SECCOMP_ADDFD_FLAG_SEND
210+
#define SECCOMP_ADDFD_FLAG_SEND (1UL << 1)
211+
#endif
212+
213+
/* SECCOMP_IOCTL_NOTIF_ADDFD was added in kernel v5.10 */
214+
#ifndef SECCOMP_IOCTL_NOTIF_ADDFD
215+
#define SECCOMP_IOCTL_NOTIF_ADDFD SECCOMP_IOW(3, \
216+
struct seccomp_notif_addfd)
217+
#endif
218+
196219
/* non-public ioctl number for backwards compat (see system.c) */
197220
#define SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR SECCOMP_IOR(2, __u64)
198221

@@ -215,4 +238,5 @@ int sys_notify_alloc(struct seccomp_notif **req,
215238
int sys_notify_receive(int fd, struct seccomp_notif *req);
216239
int sys_notify_respond(int fd, struct seccomp_notif_resp *resp);
217240
int sys_notify_id_valid(int fd, uint64_t id);
241+
int sys_notify_addfd(int fd, struct seccomp_notif_addfd *addfd);
218242
#endif

0 commit comments

Comments
 (0)