From 20143cd6b0edf4756c556ed6626d6a6c6f22fb41 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 27 Apr 2020 12:41:24 +0200 Subject: gensquashfs: Add options to globally override UID/GID values A common use case for mksquashfs is to simply pack a directory and set a magic option to force all user/group IDs to root. This commit adds similar options to gensquashfs to maek it better suited as a direct replacement for packing an input directory. Signed-off-by: David Oberhollenzer --- bin/gensquashfs/mkfs.c | 17 +++++++++++++++++ bin/gensquashfs/mkfs.h | 5 +++++ bin/gensquashfs/options.c | 31 ++++++++++++++++++++++++++++++- doc/gensquashfs.1 | 11 +++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/bin/gensquashfs/mkfs.c b/bin/gensquashfs/mkfs.c index 9ffbb94..b1542d1 100644 --- a/bin/gensquashfs/mkfs.c +++ b/bin/gensquashfs/mkfs.c @@ -171,6 +171,20 @@ static int read_fstree(fstree_t *fs, options_t *opt, sqfs_xattr_writer_t *xwr, return ret; } +static void override_owner_dfs(const options_t *opt, tree_node_t *n) +{ + if (opt->force_uid) + n->uid = opt->force_uid_value; + + if (opt->force_gid) + n->gid = opt->force_gid_value; + + if (S_ISDIR(n->mode)) { + for (n = n->data.dir.children; n != NULL; n = n->next) + override_owner_dfs(opt, n); + } +} + int main(int argc, char **argv) { int status = EXIT_FAILURE; @@ -195,6 +209,9 @@ int main(int argc, char **argv) goto out; } + if (opt.force_uid || opt.force_gid) + override_owner_dfs(&opt, sqfs.fs.root); + if (sehnd != NULL) { selinux_close_context_file(sehnd); sehnd = NULL; diff --git a/bin/gensquashfs/mkfs.h b/bin/gensquashfs/mkfs.h index 1b767aa..9a36d8c 100644 --- a/bin/gensquashfs/mkfs.h +++ b/bin/gensquashfs/mkfs.h @@ -46,6 +46,11 @@ typedef struct { const char *packdir; const char *selinux; bool no_tail_packing; + + unsigned int force_uid_value; + unsigned int force_gid_value; + bool force_uid; + bool force_gid; } options_t; enum { diff --git a/bin/gensquashfs/options.c b/bin/gensquashfs/options.c index 2369787..d029c09 100644 --- a/bin/gensquashfs/options.c +++ b/bin/gensquashfs/options.c @@ -6,7 +6,14 @@ */ #include "mkfs.h" +enum { + ALL_ROOT_OPTION = 1, +}; + static struct option long_opts[] = { + { "all-root", required_argument, NULL, ALL_ROOT_OPTION }, + { "set-uid", required_argument, NULL, 'u' }, + { "set-gid", required_argument, NULL, 'g' }, { "compressor", required_argument, NULL, 'c' }, { "block-size", required_argument, NULL, 'b' }, { "dev-block-size", required_argument, NULL, 'B' }, @@ -33,7 +40,7 @@ static struct option long_opts[] = { { NULL, 0, NULL, 0 }, }; -static const char *short_opts = "F:D:X:c:b:B:d:j:Q:kxoefqThV" +static const char *short_opts = "F:D:X:c:b:B:d:u:g:j:Q:kxoefqThV" #ifdef WITH_SELINUX "s:" #endif @@ -83,6 +90,14 @@ static const char *help_string = " mode= 0755 if not set.\n" " mtime= 0 if not set.\n" "\n" +" --set-uid, -u Force the owners user ID for ALL inodes to\n" +" this value, no matter what the pack file or\n" +" directory entries actually specify.\n" +" --set-gid, -g Force the owners group ID for ALL inodes to\n" +" this value, no matter what the pack file or\n" +" directory entries actually specify.\n" +" --all-root A short hand for `--set-uid 0 --set-gid 0`.\n" +"\n" #ifdef WITH_SELINUX " --selinux, -s Specify an SELinux label file to get context\n" " attributes from.\n" @@ -162,6 +177,20 @@ void process_command_line(options_t *opt, int argc, char **argv) break; switch (i) { + case ALL_ROOT_OPTION: + opt->force_uid_value = 0; + opt->force_gid_value = 0; + opt->force_uid = true; + opt->force_gid = true; + break; + case 'u': + opt->force_uid_value = strtol(optarg, NULL, 0); + opt->force_uid = true; + break; + case 'g': + opt->force_gid_value = strtol(optarg, NULL, 0); + opt->force_gid = true; + break; case 'T': opt->no_tail_packing = true; break; diff --git a/doc/gensquashfs.1 b/doc/gensquashfs.1 index 0de39bf..6214741 100644 --- a/doc/gensquashfs.1 +++ b/doc/gensquashfs.1 @@ -76,6 +76,17 @@ mtime=;\fB$SOURCE\_DATE\_EPOCH\fR if set, 0 otherwise .TE .TP .TP +\fB\-\-set\-uid\fR, \fB\-u\fR +Force the owners user ID for ALL inodes to this value, no matter what the pack +file or directory entries actually specify. +.TP +\fB\-\-set\-gid\fR, \fB\-g\fR +Force the owners group ID for ALL inodes to this value, no matter what the pack +file or directory entries actually specify. +.TP +\fB\-\-all\-root\fR +A short hand for `\-\-set\-uid 0 \-\-set\-gid 0`. +.TP \fB\-\-selinux\fR, \fB\-s\fR If built with SELinux support, use the given SELinux label file to add context labels to the elements packed into the SquashFS image. -- cgit v1.2.3