From 2d9fdb37fea53ba04eaa4ef99b731004ca37c2f4 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Sat, 30 Jan 2021 15:02:22 +0800 Subject: [PATCH] file: Change the owner to the operator Signed-off-by: Jianhui Zhao --- src/file.c | 18 +++++++++++++++++ src/file.h | 2 ++ src/utils.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/utils.h | 4 ++++ 4 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/file.c b/src/file.c index e4462aa..0fc8e5f 100644 --- a/src/file.c +++ b/src/file.c @@ -181,6 +181,8 @@ bool detect_file_operation(uint8_t *buf, int len, int sid, struct file_context * char fifo_name[128]; pid_t pid; int ctlfd; + uid_t uid; + gid_t gid; if (len != 12) return false; @@ -190,6 +192,16 @@ bool detect_file_operation(uint8_t *buf, int len, int sid, struct file_context * memcpy(&pid, buf + 4, 4); + if (!getuid_pid(pid, &uid)) { + kill(pid, SIGTERM); + return true; + } + + if (!getgid_pid(pid, &gid)) { + kill(pid, SIGTERM); + return true; + } + sprintf(fifo_name, "/tmp/rtty-file-%d.fifo", pid); ctlfd = open(fifo_name, O_WRONLY); @@ -218,6 +230,9 @@ bool detect_file_operation(uint8_t *buf, int len, int sid, struct file_context * memset(savepath, 0, sizeof(savepath)); getcwd_pid(pid, savepath, sizeof(savepath) - 1); strcat(savepath, "/"); + + ctx->uid = uid; + ctx->gid = gid; } else { char path[PATH_MAX] = ""; char link[128]; @@ -284,6 +299,9 @@ static void start_download_file(struct file_context *ctx, struct buffer *info, i log_info("download file: %s, size: %u\n", savepath, ctx->total_size); + if (fchown(fd, ctx->uid, ctx->gid) < 0) + log_err("fchown %s fail: %s\n", strerror(errno)); + if (ctx->total_size == 0) close(fd); else diff --git a/src/file.h b/src/file.h index ecf8548..3be2e7d 100644 --- a/src/file.h +++ b/src/file.h @@ -51,6 +51,8 @@ struct file_context { int fd; bool busy; int ctlfd; + uid_t uid; + gid_t gid; uint32_t total_size; uint32_t remain_size; struct ev_io iof; /* used for upload file */ diff --git a/src/utils.c b/src/utils.c index e8c90b6..3b5f3cd 100644 --- a/src/utils.c +++ b/src/utils.c @@ -167,4 +167,58 @@ ssize_t getcwd_pid(pid_t pid, char *buf, size_t bufsiz) sprintf(link, "/proc/%d/cwd", pid); return readlink(link, buf, bufsiz); -} \ No newline at end of file +} + +bool getuid_pid(pid_t pid, uid_t *uid) +{ + char status[128]; + char line[128]; + int i = 9; + FILE *fp; + + sprintf(status, "/proc/%d/status", pid); + + fp = fopen(status, "r"); + if (!fp) + return false; + + while (i-- > 0) { + if (!fgets(line, sizeof(line), fp)) { + fclose(fp); + return false; + } + } + + fclose(fp); + + sscanf(line, "Uid:\t%u", uid); + + return true; +} + +bool getgid_pid(pid_t pid, gid_t *gid) +{ + char status[128]; + char line[128]; + int i = 10; + FILE *fp; + + sprintf(status, "/proc/%d/status", pid); + + fp = fopen(status, "r"); + if (!fp) + return false; + + while (i-- > 0) { + if (!fgets(line, sizeof(line), fp)) { + fclose(fp); + return false; + } + } + + fclose(fp); + + sscanf(line, "Gid:\t%u", gid); + + return true; +} diff --git a/src/utils.h b/src/utils.h index c7c14db..822ef7e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -40,4 +40,8 @@ struct mntent *find_mount_point(const char *name); ssize_t getcwd_pid(pid_t pid, char *buf, size_t bufsiz); +bool getuid_pid(pid_t pid, uid_t *uid); + +bool getgid_pid(pid_t pid, gid_t *gid); + #endif