Skip to content

Commit a034638

Browse files
realsdxpcmoore
authored andcommitted
api: add python api for seccomp notify addfd
Add seccomp notify addfd support to the Python API and update the receive_notify and respond_notify methods to accept user-provided notify fd's as an optional argument. Signed-off-by: Sudipta Pandit <sudpandit@microsoft.com> [PM: line wrap description, fix trailing whitespace, squash rename patch] Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 6e12822 commit a034638

2 files changed

Lines changed: 179 additions & 4 deletions

File tree

src/python/libseccomp.pxd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ cdef extern from "seccomp.h":
199199
int32_t error
200200
uint32_t flags
201201

202+
cdef struct seccomp_notif_addfd:
203+
uint64_t id
204+
uint32_t flags
205+
uint32_t srcfd
206+
uint32_t newfd
207+
uint32_t newfd_flags
208+
202209
scmp_version *seccomp_version()
203210

204211
unsigned int seccomp_api_get()
@@ -247,6 +254,7 @@ cdef extern from "seccomp.h":
247254
void seccomp_notify_free(seccomp_notif *req, seccomp_notif_resp *resp)
248255
int seccomp_notify_receive(int fd, seccomp_notif *req)
249256
int seccomp_notify_respond(int fd, seccomp_notif_resp *resp)
257+
int seccomp_notify_addfd(int fd, seccomp_notif_addfd *addfd)
250258
int seccomp_notify_id_valid(int fd, uint64_t id)
251259
int seccomp_notify_fd(scmp_filter_ctx ctx)
252260

src/python/seccomp.pyx

Lines changed: 171 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,139 @@ cdef class NotificationResponse:
591591
"""
592592
self._flags = value
593593

594+
cdef class NotificationAddfd:
595+
""" Python object representing a seccomp notification addfd structure.
596+
"""
597+
cdef uint64_t _id
598+
cdef uint32_t _flags
599+
cdef uint32_t _srcfd
600+
cdef uint32_t _newfd
601+
cdef uint32_t _newfd_flags
602+
603+
def __cinit__(self, notify, flags, srcfd, newfd = 0, newfd_flags = 0):
604+
""" Initialize the notification addfd structure.
605+
606+
Arguments:
607+
notify - a Notification object
608+
srcfd - the source file descriptor
609+
flags - notify addfd flags
610+
newfd - 0 or desired file descriptor number in target
611+
newfd_flags - new flags to set on the target file descriptor
612+
613+
Description:
614+
Create a seccomp NotificationAddfd object.
615+
"""
616+
self._id = notify.id
617+
self._flags = flags
618+
self._srcfd = srcfd
619+
self._newfd = newfd
620+
self._newfd_flags = newfd_flags
621+
622+
@property
623+
def id(self):
624+
""" Get the seccomp notification request ID.
625+
626+
Description:
627+
Get the seccomp notification request ID.
628+
"""
629+
return self._id
630+
631+
@id.setter
632+
def id(self, value):
633+
""" Set the seccomp notification request ID.
634+
635+
Arguments:
636+
id - the seccomp notification request ID
637+
638+
Description:
639+
Set the seccomp notification request ID.
640+
"""
641+
self._id = value
642+
643+
@property
644+
def flags(self):
645+
""" Get the seccomp notification addfd flags.
646+
647+
Description:
648+
Get the seccomp notification addfd flags.
649+
"""
650+
return self._flags
651+
652+
@flags.setter
653+
def flags(self, value):
654+
""" Set the seccomp notification addfd flags.
655+
656+
Arguments:
657+
flags - the notification addfd flags
658+
659+
Description:
660+
Set the seccomp notification addfd flags.
661+
"""
662+
self._flags = value
663+
664+
@property
665+
def srcfd(self):
666+
""" Get the local file descriptor number.
667+
668+
Description:
669+
Get the local file descriptor number.
670+
"""
671+
return self._srcfd
672+
673+
@srcfd.setter
674+
def srcfd(self, value):
675+
""" Set the local file descriptor number.
676+
677+
Arguments:
678+
srcfd - the local file descriptor number
679+
680+
Description:
681+
Set the local file descriptor number.
682+
"""
683+
self._srcfd = value
684+
685+
@property
686+
def newfd(self):
687+
""" Get the target file descriptor number.
688+
689+
Description:
690+
Get the target file descriptor number.
691+
"""
692+
return self._newfd
693+
694+
@newfd.setter
695+
def newfd(self, value):
696+
""" Set the target file descriptor number.
697+
698+
Arguments:
699+
newfd - the target file descriptor number
700+
701+
Description:
702+
Set the target file descriptor number.
703+
"""
704+
self._newfd = value
705+
706+
@property
707+
def newfd_flags(self):
708+
""" Get the new flags to set on the target file descriptor.
709+
710+
Description:
711+
Get the new flags to set on the target file descriptor.
712+
"""
713+
return self._newfd_flags
714+
715+
@newfd_flags.setter
716+
def newfd_flags(self, value):
717+
""" Set the new flags to set on the target file descriptor.
718+
719+
Arguments:
720+
newfd_flags - the new flags to set on the target file descriptor
721+
722+
Description:
723+
Set the new flags to set on the target file descriptor.
724+
"""
725+
self._newfd_flags = value
726+
594727
cdef class SyscallFilter:
595728
""" Python object representing a seccomp syscall filter. """
596729
cdef int _defaction
@@ -959,16 +1092,20 @@ cdef class SyscallFilter:
9591092
if rc != 0:
9601093
raise RuntimeError(str.format("Library error (errno = {0})", rc))
9611094

962-
def receive_notify(self):
1095+
def receive_notify(self, fd = None):
9631096
""" Receive seccomp notifications.
9641097
1098+
Arguments:
1099+
fd - the notify file descriptor
1100+
9651101
Description:
9661102
Receive a seccomp notification from the system, requires the use of
9671103
the NOTIFY action.
9681104
"""
9691105
cdef libseccomp.seccomp_notif *req
9701106

971-
fd = libseccomp.seccomp_notify_fd(self._ctx)
1107+
if fd is None:
1108+
fd = libseccomp.seccomp_notify_fd(self._ctx)
9721109
if fd < 0:
9731110
raise RuntimeError("Notifications not enabled/active")
9741111
rc = libseccomp.seccomp_notify_alloc(&req, NULL)
@@ -988,18 +1125,20 @@ cdef class SyscallFilter:
9881125
free(req)
9891126
return notify
9901127

991-
def respond_notify(self, response):
1128+
def respond_notify(self, response, fd = None):
9921129
""" Send a seccomp notification response.
9931130
9941131
Arguments:
9951132
response - the response to send to the system
1133+
fd - the notify file descriptor
9961134
9971135
Description:
9981136
Respond to a seccomp notification.
9991137
"""
10001138
cdef libseccomp.seccomp_notif_resp *resp
10011139

1002-
fd = libseccomp.seccomp_notify_fd(self._ctx)
1140+
if fd is None:
1141+
fd = libseccomp.seccomp_notify_fd(self._ctx)
10031142
if fd < 0:
10041143
raise RuntimeError("Notifications not enabled/active")
10051144
rc = libseccomp.seccomp_notify_alloc(NULL, &resp)
@@ -1026,6 +1165,34 @@ cdef class SyscallFilter:
10261165
raise RuntimeError("Notifications not enabled/active")
10271166
return fd
10281167

1168+
def notify_addfd(self, addfd, fd = None):
1169+
"""Add a file descriptor to target
1170+
1171+
Arguments:
1172+
addfd - the addfd object
1173+
fd - the notify file descriptor
1174+
1175+
Description:
1176+
Add a file descriptor to the target process.
1177+
"""
1178+
if fd is None:
1179+
fd = libseccomp.seccomp_notify_fd(self._ctx)
1180+
if fd < 0:
1181+
raise RuntimeError("Notifications not enabled/active")
1182+
1183+
cdef libseccomp.seccomp_notif_addfd _addfd
1184+
1185+
_addfd.id = addfd.id
1186+
_addfd.flags = addfd.flags
1187+
_addfd.srcfd = addfd.srcfd
1188+
_addfd.newfd = addfd.newfd
1189+
_addfd.newfd_flags = addfd.newfd_flags
1190+
1191+
rc = libseccomp.seccomp_notify_addfd(fd, &_addfd)
1192+
if rc < 0:
1193+
raise RuntimeError(str.format("Library error (errno = {0})", rc))
1194+
return rc
1195+
10291196
def export_pfc(self, file):
10301197
""" Export the filter in PFC format.
10311198

0 commit comments

Comments
 (0)