Commit 4286ee8b authored by zhul's avatar zhul 🌵

Switch to internal API and add fallback user for fstatat()

- Add `fstatat_with_fallback_user` function to handle UIDs in `my_fstatat.c`
- Add `#include <stdbool.h>` and `bool retried` to `proc_parse.c`
- Add `retry_o_path` goto and `if (!retried)` check to `proc_parse.c`
- Use `fstatat_with_fallback_user

[criu/my_fstatat.c]
- Add `fstatat_with_fallback_user` function
- Set the UID based on the `CURRENT_TASK_USER` environment variable
- Rollback to the original UID after the call to `fstatat`
[criu/proc_parse.c]
- Add `#include <stdbool.h>`
- Add `bool retried`
- Add `retry_o_path` goto
- Add `if (!retried)` check
- Add `flags = O_PATH`
[criu/files-reg.c]
- Include `my_fstatat.c`
- Use `fstatat_with_fallback_user` instead of `fstatat`
parent 437561d2
Pipeline #286 canceled with stages
...@@ -44,6 +44,8 @@ ...@@ -44,6 +44,8 @@
#include "files-reg.h" #include "files-reg.h"
#include "plugin.h" #include "plugin.h"
#include "my_fstatat.c"
int setfsuid(uid_t fsuid); int setfsuid(uid_t fsuid);
int setfsgid(gid_t fsuid); int setfsgid(gid_t fsuid);
...@@ -1206,7 +1208,7 @@ static int check_path_remap(struct fd_link *link, const struct fd_parms *parms, ...@@ -1206,7 +1208,7 @@ static int check_path_remap(struct fd_link *link, const struct fd_parms *parms,
if (mntns_root < 0) if (mntns_root < 0)
return -1; return -1;
ret = fstatat(mntns_root, rpath, &pst, 0); ret = fstatat_with_fallback_user(mntns_root, rpath, &pst, 0);
if (ret < 0) { if (ret < 0) {
/* /*
* Linked file, but path is not accessible (unless any * Linked file, but path is not accessible (unless any
......
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <sys/stat.h>
static int fstatat_with_fallback_user (int __fd, const char *__restrict __file,
struct stat *__restrict __buf, int __flag) {
int rst;
rst = fstatat(__fd, __file, __buf, __flag);
if (rst >= 0) {
return rst;
}
char *current_task_user = getenv("CURRENT_TASK_USER");
if (current_task_user == NULL) {
fprintf(stderr, "Error: no env CURRENT_TASK_USER '%s'\n", current_task_user);
return rst;
}
struct passwd *pw = getpwnam(current_task_user);
if (pw == NULL) {
fprintf(stderr, "Error: no such user '%s'\n", current_task_user);
return rst;
}
uid_t uid = pw->pw_uid;
printf("UID for user '%s' is %d\n", current_task_user, uid);
seteuid(uid);
rst = fstatat(__fd, __file, __buf, __flag);
if (seteuid(getuid()) == -1) {
fprintf(stderr, "Error: rollback to original uid failed'\n");
}
return rst;
}
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -352,6 +353,7 @@ static int vma_get_mapfile(const char *fname, struct vma_area *vma, DIR *mfd, ...@@ -352,6 +353,7 @@ static int vma_get_mapfile(const char *fname, struct vma_area *vma, DIR *mfd,
{ {
char path[32]; char path[32];
int flags; int flags;
bool retried;
/* Figure out if it's file mapping */ /* Figure out if it's file mapping */
snprintf(path, sizeof(path), "%"PRIx64"-%"PRIx64, vma->e->start, vma->e->end); snprintf(path, sizeof(path), "%"PRIx64"-%"PRIx64, vma->e->start, vma->e->end);
...@@ -403,6 +405,7 @@ static int vma_get_mapfile(const char *fname, struct vma_area *vma, DIR *mfd, ...@@ -403,6 +405,7 @@ static int vma_get_mapfile(const char *fname, struct vma_area *vma, DIR *mfd,
*/ */
flags = O_RDONLY; flags = O_RDONLY;
retry_o_path:
*vm_file_fd = openat(dirfd(mfd), path, flags); *vm_file_fd = openat(dirfd(mfd), path, flags);
if (*vm_file_fd < 0) { if (*vm_file_fd < 0) {
if (errno == ENOENT) if (errno == ENOENT)
...@@ -437,6 +440,12 @@ static int vma_get_mapfile(const char *fname, struct vma_area *vma, DIR *mfd, ...@@ -437,6 +440,12 @@ static int vma_get_mapfile(const char *fname, struct vma_area *vma, DIR *mfd,
return vma_get_mapfile_user(fname, vma, vfi, vm_file_fd, path); return vma_get_mapfile_user(fname, vma, vfi, vm_file_fd, path);
pr_perror("Can't open map_files"); pr_perror("Can't open map_files");
if (!retried) {
flags = O_PATH;
retried = true;
goto retry_o_path;
}
return -1; return -1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment