Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: use /proc/self/fd to open unix socket #652

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion contrib/seccomp-notify-plugin-rust/src/seccompwatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type Errno = i32;

type SyscallHandler = fn(req: &mut nc::seccomp_notif_t) -> Result<bool, Errno>;

#[no_mangle]
pub struct LibcrunLoadSeccompNotifyConf {
_runtime_root_path: *const c_char,
_name: *const c_char,
Expand Down
22 changes: 19 additions & 3 deletions src/libcrun/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -878,12 +878,24 @@ open_unix_domain_client_socket (const char *path, int dgram, libcrun_error_t *er
{
struct sockaddr_un addr = {};
int ret;
cleanup_close int fd = socket (AF_UNIX, dgram ? SOCK_DGRAM : SOCK_STREAM, 0);
char name_buf[32];
cleanup_close int destfd = -1;
cleanup_close int fd = -1;

fd = socket (AF_UNIX, dgram ? SOCK_DGRAM : SOCK_STREAM, 0);
if (UNLIKELY (fd < 0))
return crun_make_error (err, errno, "error creating UNIX socket");

if (strlen (path) >= sizeof (addr.sun_path))
return crun_make_error (err, 0, "invalid path %s specified", path);
{
destfd = open (path, O_PATH);
if (UNLIKELY (destfd < 0))
return crun_make_error (err, errno, "error opening `%s`", path);

sprintf (name_buf, "/proc/self/fd/%d", destfd);
path = name_buf;
}

strcpy (addr.sun_path, path);
addr.sun_family = AF_UNIX;
ret = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
Expand All @@ -901,12 +913,16 @@ open_unix_domain_socket (const char *path, int dgram, libcrun_error_t *err)
{
struct sockaddr_un addr = {};
int ret;
char name_buf[32];
cleanup_close int fd = socket (AF_UNIX, dgram ? SOCK_DGRAM : SOCK_STREAM, 0);
if (UNLIKELY (fd < 0))
return crun_make_error (err, errno, "error creating UNIX socket");

if (strlen (path) >= sizeof (addr.sun_path))
return crun_make_error (err, 0, "invalid path %s specified", path);
{
sprintf (name_buf, "/proc/self/fd/%d", fd);
path = name_buf;
}
strcpy (addr.sun_path, path);
addr.sun_family = AF_UNIX;
ret = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
Expand Down