Skip to content

Commit

Permalink
Introduce vfcntl()
Browse files Browse the repository at this point in the history
This is a variant of fcntl that takes a va_list instead of variadic
arguments and can be used to safely interpose fcntl by libraries such
as epoll-shim that would otherwise have to reimplement the logic inside
fcntl.c to avoid reading incorrect arguments for CheriBSD.

See jiixyj/epoll-shim#36
  • Loading branch information
arichardson committed Aug 11, 2024
1 parent ead347f commit 8da52ee
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/libsys/Symbol.sys.map
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ FBSD_1.7 {
};

FBSD_1.8 {
kcmp;
kcmp;
vfcntl;
};

FBSDprivate_1.0 {
Expand Down
3 changes: 3 additions & 0 deletions lib/libsys/fcntl.2
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
.In fcntl.h
.Ft int
.Fn fcntl "int fd" "int cmd" "..."
.In stdarg.h
.Ft int
.Fn vfcntl "int fd" "int cmd" "va_list ap"
.Sh DESCRIPTION
The
.Fn fcntl
Expand Down
18 changes: 14 additions & 4 deletions lib/libsys/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
* CHERI CHANGES END
*/

#include <fcntl.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/syscall.h>

#include <fcntl.h>
#include <stdarg.h>

#include "libc_private.h"

typedef int (*fcntl_func)(int, int, intptr_t);
Expand Down Expand Up @@ -80,16 +82,24 @@ call_fcntl(fcntl_func fn, int fd, int cmd, va_list ap)
return (fn(fd, cmd, arg));
}

#pragma weak fcntl
int
vfcntl(int fd, int cmd, va_list ap)
{
fcntl_func fn = (fcntl_func)*__libsys_interposing_slot(INTERPOS_fcntl);

return (call_fcntl(fn, fd, cmd, ap));
}

#pragma weak fcntl
int
fcntl(int fd, int cmd, ...)
{
va_list args;
int result;
fcntl_func fn = (fcntl_func)*__libsys_interposing_slot(INTERPOS_fcntl);

va_start(args, cmd);
result = call_fcntl(fn, fd, cmd, args);
result = vfcntl(fd, cmd, args);
va_end(args);

return (result);
Expand Down
1 change: 1 addition & 0 deletions sys/sys/fcntl.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ int open(const char *, int, ...);
int creat(const char *, mode_t);
int fcntl(int, int, ...);
#if __BSD_VISIBLE
int vfcntl(int, int, __va_list);
int flock(int, int);
int fspacectl(int, int, const struct spacectl_range *, int,
struct spacectl_range *);

Check failure on line 382 in sys/sys/fcntl.h

View workflow job for this annotation

GitHub Actions / Style Checker

Missing Signed-off-by: line
Expand Down

0 comments on commit 8da52ee

Please sign in to comment.