fix(utils): retry fgetpwent_r() on EINTR#2043
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a potential issue where user lookup operations could fail intermittently due to signal interruptions. By introducing a retry loop for the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly fixes an issue where fgetpwent_r() could fail when interrupted by a signal by retrying on EINTR. The change is correct and follows standard practice. My review includes a suggestion to refactor the retry logic to make it more idiomatic and self-contained, which would improve code clarity.
kolyshkin
left a comment
There was a problem hiding this comment.
I think this is a fix to something that's not a bug; see #2009 (comment)
|
@kolyshkin Thanks for the context. It is IMO a real bug in uncommon setups, imagine e.g. rootless containers setup with fuse-overlayfs. The FUSE read() can return EINTR because the kernel FUSE driver uses interruptible waits. The glibc's stdio layer does not retry on EINTR internally. So EINTR can propagate all the way up to fgetpwent_r's return value. Does it make sense? |
|
@kolyshkin I had a quick look at glibc and I don't seem to find any retry on #define _GNU_SOURCE
#include <errno.h>
#include <linux/audit.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <pwd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>
#if __x86_64__
#define AUDIT_ARCH_CURRENT AUDIT_ARCH_X86_64
#elif __aarch64__
#define AUDIT_ARCH_CURRENT AUDIT_ARCH_AARCH64
#else
#error "Unsupported architecture"
#endif
static unsigned long n_calls;
static int install_seccomp_filter(void) {
struct sock_filter filter[] = {
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch)),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_CURRENT, 1, 0),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_read, 0, 1),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_USER_NOTIF),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
};
struct sock_fprog prog = {
.len = sizeof(filter) / sizeof(filter[0]),
.filter = filter,
};
return syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER,
SECCOMP_FILTER_FLAG_NEW_LISTENER, &prog);
}
/* Send an fd over a unix socket */
static int send_fd(int sock, int fd) {
char buf = 0;
struct iovec iov = {.iov_base = &buf, .iov_len = 1};
union {
struct cmsghdr hdr;
char bytes[CMSG_SPACE(sizeof(int))];
} cmsg_buf;
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsg_buf.bytes,
.msg_controllen = sizeof(cmsg_buf.bytes),
};
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
return sendmsg(sock, &msg, 0) >= 0 ? 0 : -1;
}
/* Receive an fd over a unix socket */
static int recv_fd(int sock) {
char buf;
struct iovec iov = {.iov_base = &buf, .iov_len = 1};
union {
struct cmsghdr hdr;
char bytes[CMSG_SPACE(sizeof(int))];
} cmsg_buf;
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsg_buf.bytes,
.msg_controllen = sizeof(cmsg_buf.bytes),
};
if (recvmsg(sock, &msg, 0) < 0)
return -1;
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
int fd;
memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
return fd;
}
static void run_child(int sock) {
FILE *f = fopen("/etc/passwd", "r");
if (!f) {
perror("fopen");
_exit(1);
}
/*
* Install seccomp AFTER all setup is done (dynamic linker,
* fopen, etc.), so only the fgetpwent_r reads are intercepted.
*/
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
perror("prctl(NO_NEW_PRIVS)");
_exit(1);
}
int notify_fd = install_seccomp_filter();
if (notify_fd < 0) {
perror("seccomp");
_exit(1);
}
/* Pass the notify fd to the parent */
send_fd(sock, notify_fd);
close(notify_fd);
close(sock);
struct passwd pw;
char buf[4096];
struct passwd *result;
int ret;
int count = 0;
while (1) {
ret = fgetpwent_r(f, &pw, buf, sizeof(buf), &result);
if (ret)
break;
count++;
printf("%-20s uid=%-5u gid=%-5u home=%s shell=%s\n", pw.pw_name, pw.pw_uid,
pw.pw_gid, pw.pw_dir, pw.pw_shell);
}
if (ret != 0 && ret != ENOENT)
fprintf(stderr, "fgetpwent_r failed: %s (%d)\n", strerror(ret), ret);
fprintf(stderr, "Total entries: %d\n", count);
fclose(f);
_exit(0);
}
static void run_supervisor(int notify_fd, pid_t child) {
struct seccomp_notif_sizes sizes;
if (syscall(__NR_seccomp, SECCOMP_GET_NOTIF_SIZES, 0, &sizes)) {
perror("seccomp(GET_NOTIF_SIZES)");
return;
}
for (;;) {
struct seccomp_notif *req = calloc(1, sizes.seccomp_notif);
struct seccomp_notif_resp *resp = calloc(1, sizes.seccomp_notif_resp);
if (ioctl(notify_fd, SECCOMP_IOCTL_NOTIF_RECV, req)) {
free(req);
free(resp);
break;
}
resp->id = req->id;
if (n_calls % 2 == 0) {
resp->error = -EINTR;
resp->val = 0;
resp->flags = 0;
fprintf(stderr, "[supervisor] read(fd=%d) call #%lu -> EINTR\n",
(int)req->data.args[0], n_calls);
} else {
resp->error = 0;
resp->val = 0;
resp->flags = SECCOMP_USER_NOTIF_FLAG_CONTINUE;
fprintf(stderr, "[supervisor] read(fd=%d) call #%lu -> CONTINUE\n",
(int)req->data.args[0], n_calls);
}
n_calls++;
if (ioctl(notify_fd, SECCOMP_IOCTL_NOTIF_SEND, resp)) {
if (errno != ENOENT)
perror("NOTIF_SEND");
}
free(req);
free(resp);
}
int status;
waitpid(child, &status, 0);
if (WIFEXITED(status))
fprintf(stderr, "[supervisor] child exited with %d\n", WEXITSTATUS(status));
}
int main(void) {
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) {
perror("socketpair");
return 1;
}
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return 1;
}
if (pid == 0) {
close(sv[0]);
run_child(sv[1]);
_exit(0);
}
close(sv[1]);
int notify_fd = recv_fd(sv[0]);
close(sv[0]);
if (notify_fd < 0) {
fprintf(stderr, "failed to receive notify fd\n");
return 1;
}
run_supervisor(notify_fd, pid);
close(notify_fd);
return 0;
}and then: |
|
@jnovy could you please rebase? |
giuseppe
left a comment
There was a problem hiding this comment.
LGTM,
but let's wait for @kolyshkin to chime in
|
Oh well :( FWIW glibc docs say
and EINTR is listed for open/read/write/close etc., but not for e.g. getline/fgets OTOH it also says
I guess glibc implementation is among those "many". Perhaps there are many other places that need retry-on-EINTR logic. |
The passwd entry lookup could fail spuriously when interrupted by a signal during iteration. The call is now retried when interrupted, consistent with standard practice for handling interruptible system calls. Fixes: containers#2009 Signed-off-by: Jindrich Novy <jnovy@redhat.com>
The passwd entry lookup could fail spuriously when interrupted by a signal during iteration. The call is now retried when interrupted, consistent with standard practice for handling interruptible system calls.
Fixes: #2009