Created
November 6, 2019 21:50
-
-
Save jonathonf/88ccc50f12d4f5c0575da2924ee8f2fb to your computer and use it in GitHub Desktop.
Make-MVM_file_is_rwx-honor-root-and-groups.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 36c35db159ec676206d190f58dd14131e9d51844 Mon Sep 17 00:00:00 2001 | |
From: =?UTF-8?q?Patrick=20B=C3=B6ker?= <patzim@users.noreply.github.com> | |
Date: Mon, 14 Oct 2019 20:17:46 +0200 | |
Subject: [PATCH] Make MVM_file_is_rwx honor root and groups | |
--- | |
src/io/fileops.c | 32 +++++++++++++++++++++++++++++++- | |
1 file changed, 31 insertions(+), 1 deletion(-) | |
diff --git a/src/io/fileops.c b/src/io/fileops.c | |
index 613c05973..61583b4d8 100644 | |
--- a/src/io/fileops.c | |
+++ b/src/io/fileops.c | |
@@ -246,6 +246,35 @@ MVMint64 MVM_file_isexecutable(MVMThreadContext *tc, MVMString *filename, MVMint | |
} | |
} | |
#else | |
+ | |
+static int are_we_group_member(MVMThreadContext *tc, gid_t group) { | |
+ int len; | |
+ gid_t *gids; | |
+ int res; | |
+ int i; | |
+ /* Check the user group. */ | |
+ if (getegid() == group) | |
+ return 1; | |
+ /* Check the supplementary groups. */ | |
+ len = getgroups(0, NULL); | |
+ if (len == 0) | |
+ return 0; | |
+ gids = MVM_malloc(len * sizeof(gid_t)); | |
+ res = getgroups(len, gids); | |
+ if (res < 0) { | |
+ MVM_free(gids); | |
+ MVM_exception_throw_adhoc(tc, "Failed to retrieve groups: %s", strerror(errno)); | |
+ } | |
+ res = 0; | |
+ for (i = 0; i < len; i++) { | |
+ if (gids[i] == group) { | |
+ res = 1; | |
+ break; | |
+ } | |
+ } | |
+ MVM_free(gids); | |
+ return res; | |
+} | |
#define FILE_IS(name, rwx) \ | |
MVMint64 MVM_file_is ## name (MVMThreadContext *tc, MVMString *filename, MVMint32 use_lstat) { \ | |
if (!MVM_file_exists(tc, filename, use_lstat)) \ | |
@@ -254,7 +283,8 @@ MVMint64 MVM_file_isexecutable(MVMThreadContext *tc, MVMString *filename, MVMint | |
uv_stat_t statbuf = file_info(tc, filename, use_lstat); \ | |
MVMint64 r = (statbuf.st_mode & S_I ## rwx ## OTH) \ | |
|| (statbuf.st_uid == geteuid() && (statbuf.st_mode & S_I ## rwx ## USR)) \ | |
- || (statbuf.st_uid == getegid() && (statbuf.st_mode & S_I ## rwx ## GRP)); \ | |
+ || (geteuid() == 0) \ | |
+ || (are_we_group_member(tc, statbuf.st_gid) && (statbuf.st_mode & S_I ## rwx ## GRP)); \ | |
return r ? 1 : 0; \ | |
} \ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment