From dce63f71c4b901e776686cca351d4ff55badb425 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 20 Oct 2023 11:44:27 +0200 Subject: Cleanup: gensquashfs: merge xattr scanning code The fstree from file and directory xattr scanning code essentially do the same thing now. Except the later also _optionally_ reads xattrs from a directory source. Merge the two code paths. Signed-off-by: David Oberhollenzer --- bin/gensquashfs/Makemodule.am | 2 +- bin/gensquashfs/src/apply_xattr.c | 220 ++++++++++++++++++++++++++++++++++++ bin/gensquashfs/src/dirscan_xattr.c | 220 ------------------------------------ bin/gensquashfs/src/mkfs.c | 70 +----------- bin/gensquashfs/src/mkfs.h | 4 +- 5 files changed, 227 insertions(+), 289 deletions(-) create mode 100644 bin/gensquashfs/src/apply_xattr.c delete mode 100644 bin/gensquashfs/src/dirscan_xattr.c diff --git a/bin/gensquashfs/Makemodule.am b/bin/gensquashfs/Makemodule.am index 5246abf..f983ccf 100644 --- a/bin/gensquashfs/Makemodule.am +++ b/bin/gensquashfs/Makemodule.am @@ -1,6 +1,6 @@ gensquashfs_SOURCES = bin/gensquashfs/src/mkfs.c bin/gensquashfs/src/mkfs.h \ bin/gensquashfs/src/options.c bin/gensquashfs/src/selinux.c \ - bin/gensquashfs/src/dirscan_xattr.c bin/gensquashfs/src/filemap_xattr.c\ + bin/gensquashfs/src/apply_xattr.c bin/gensquashfs/src/filemap_xattr.c\ bin/gensquashfs/src/fstree_from_file.c \ bin/gensquashfs/src/fstree_from_dir.c \ bin/gensquashfs/src/sort_by_file.c bin/gensquashfs/src/glob.c diff --git a/bin/gensquashfs/src/apply_xattr.c b/bin/gensquashfs/src/apply_xattr.c new file mode 100644 index 0000000..cc03eab --- /dev/null +++ b/bin/gensquashfs/src/apply_xattr.c @@ -0,0 +1,220 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * apply_xattr.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "mkfs.h" + +#ifdef HAVE_SYS_XATTR_H +static char *get_full_path(const char *prefix, tree_node_t *node) +{ + char *path = NULL, *new = NULL; + size_t path_len, prefix_len; + int ret; + + path = fstree_get_path(node); + if (path == NULL) + goto fail; + + ret = canonicalize_name(path); + assert(ret == 0); + + path_len = strlen(path); + prefix_len = strlen(prefix); + + while (prefix_len > 0 && prefix[prefix_len - 1] == '/') + --prefix_len; + + if (prefix_len > 0) { + new = realloc(path, path_len + prefix_len + 2); + if (new == NULL) + goto fail; + + path = new; + + memmove(path + prefix_len + 1, path, path_len + 1); + memcpy(path, prefix, prefix_len); + path[prefix_len] = '/'; + } + + return path; +fail: + perror("getting full path for xattr scan"); + free(path); + return NULL; +} + +static int xattr_from_path(sqfs_xattr_writer_t *xwr, const char *path) +{ + char *key, *value = NULL, *buffer = NULL; + ssize_t buflen, vallen, keylen; + int ret; + + buflen = llistxattr(path, NULL, 0); + if (buflen < 0) { + fprintf(stderr, "llistxattr %s: %s", path, strerror(errno)); + return -1; + } + + if (buflen == 0) + return 0; + + buffer = malloc(buflen); + if (buffer == NULL) { + perror("xattr name buffer"); + return -1; + } + + buflen = llistxattr(path, buffer, buflen); + if (buflen == -1) { + fprintf(stderr, "llistxattr %s: %s", path, strerror(errno)); + goto fail; + } + + key = buffer; + while (buflen > 0) { + vallen = lgetxattr(path, key, NULL, 0); + if (vallen == -1) { + fprintf(stderr, "lgetxattr %s: %s", + path, strerror(errno)); + goto fail; + } + + if (vallen > 0) { + value = calloc(1, vallen); + if (value == NULL) { + perror("allocating xattr value buffer"); + goto fail; + } + + vallen = lgetxattr(path, key, value, vallen); + if (vallen == -1) { + fprintf(stderr, "lgetxattr %s: %s\n", + path, strerror(errno)); + goto fail; + } + + ret = sqfs_xattr_writer_add_kv(xwr, key, value, vallen); + if (ret) { + sqfs_perror(path, + "storing xattr key-value pairs", + ret); + goto fail; + } + + free(value); + value = NULL; + } + + keylen = strlen(key) + 1; + buflen -= keylen; + key += keylen; + } + + free(buffer); + return 0; +fail: + free(value); + free(buffer); + return -1; +} +#endif + +static int xattr_xcan_dfs(const char *path_prefix, void *selinux_handle, + sqfs_xattr_writer_t *xwr, bool scan_xattr, void *xattr_map, + tree_node_t *node) +{ + char *path = NULL; + int ret; + + ret = sqfs_xattr_writer_begin(xwr, 0); + if (ret) { + sqfs_perror(node->name, "recoding xattr key-value pairs\n", + ret); + return -1; + } + +#ifdef HAVE_SYS_XATTR_H + if (scan_xattr) { + path = get_full_path(path_prefix, node); + if (path == NULL) + return -1; + + ret = xattr_from_path(xwr, path); + free(path); + path = NULL; + if (ret) { + ret = -1; + goto out; + } + } +#else + (void)path_prefix; +#endif + + if (selinux_handle != NULL || xattr_map != NULL) { + path = fstree_get_path(node); + + if (path == NULL) { + perror("reconstructing absolute path"); + ret = -1; + goto out; + } + } + + if (xattr_map != NULL) { + ret = xattr_apply_map_file(path, xattr_map, xwr); + + if (ret) { + ret = -1; + goto out; + } + } + + if (selinux_handle != NULL) { + ret = selinux_relable_node(selinux_handle, xwr, node, path); + + if (ret) { + ret = -1; + goto out; + } + } + + if (sqfs_xattr_writer_end(xwr, &node->xattr_idx)) { + sqfs_perror(node->name, "completing xattr key-value pairs", + ret); + ret = -1; + goto out; + } + + if (S_ISDIR(node->mode)) { + node = node->data.children; + + while (node != NULL) { + if (xattr_xcan_dfs(path_prefix, selinux_handle, xwr, + scan_xattr, xattr_map, node)) { + ret = -1; + goto out; + } + + node = node->next; + } + } + +out: + free(path); + return ret; +} + +int apply_xattrs(fstree_t *fs, const char *path, void *selinux_handle, + void *xattr_map, sqfs_xattr_writer_t *xwr, bool scan_xattr) +{ + if (xwr == NULL) + return 0; + + if (selinux_handle == NULL && !scan_xattr && xattr_map == NULL) + return 0; + + return xattr_xcan_dfs(path, selinux_handle, xwr, scan_xattr, xattr_map, fs->root); +} diff --git a/bin/gensquashfs/src/dirscan_xattr.c b/bin/gensquashfs/src/dirscan_xattr.c deleted file mode 100644 index 1bc829f..0000000 --- a/bin/gensquashfs/src/dirscan_xattr.c +++ /dev/null @@ -1,220 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * dirscan_xattr.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "mkfs.h" - -#ifdef HAVE_SYS_XATTR_H -static char *get_full_path(const char *prefix, tree_node_t *node) -{ - char *path = NULL, *new = NULL; - size_t path_len, prefix_len; - int ret; - - path = fstree_get_path(node); - if (path == NULL) - goto fail; - - ret = canonicalize_name(path); - assert(ret == 0); - - path_len = strlen(path); - prefix_len = strlen(prefix); - - while (prefix_len > 0 && prefix[prefix_len - 1] == '/') - --prefix_len; - - if (prefix_len > 0) { - new = realloc(path, path_len + prefix_len + 2); - if (new == NULL) - goto fail; - - path = new; - - memmove(path + prefix_len + 1, path, path_len + 1); - memcpy(path, prefix, prefix_len); - path[prefix_len] = '/'; - } - - return path; -fail: - perror("getting full path for xattr scan"); - free(path); - return NULL; -} - -static int xattr_from_path(sqfs_xattr_writer_t *xwr, const char *path) -{ - char *key, *value = NULL, *buffer = NULL; - ssize_t buflen, vallen, keylen; - int ret; - - buflen = llistxattr(path, NULL, 0); - if (buflen < 0) { - fprintf(stderr, "llistxattr %s: %s", path, strerror(errno)); - return -1; - } - - if (buflen == 0) - return 0; - - buffer = malloc(buflen); - if (buffer == NULL) { - perror("xattr name buffer"); - return -1; - } - - buflen = llistxattr(path, buffer, buflen); - if (buflen == -1) { - fprintf(stderr, "llistxattr %s: %s", path, strerror(errno)); - goto fail; - } - - key = buffer; - while (buflen > 0) { - vallen = lgetxattr(path, key, NULL, 0); - if (vallen == -1) { - fprintf(stderr, "lgetxattr %s: %s", - path, strerror(errno)); - goto fail; - } - - if (vallen > 0) { - value = calloc(1, vallen); - if (value == NULL) { - perror("allocating xattr value buffer"); - goto fail; - } - - vallen = lgetxattr(path, key, value, vallen); - if (vallen == -1) { - fprintf(stderr, "lgetxattr %s: %s\n", - path, strerror(errno)); - goto fail; - } - - ret = sqfs_xattr_writer_add_kv(xwr, key, value, vallen); - if (ret) { - sqfs_perror(path, - "storing xattr key-value pairs", - ret); - goto fail; - } - - free(value); - value = NULL; - } - - keylen = strlen(key) + 1; - buflen -= keylen; - key += keylen; - } - - free(buffer); - return 0; -fail: - free(value); - free(buffer); - return -1; -} -#endif - -static int xattr_xcan_dfs(const char *path_prefix, void *selinux_handle, - sqfs_xattr_writer_t *xwr, bool scan_xattr, void *xattr_map, - tree_node_t *node) -{ - char *path = NULL; - int ret; - - ret = sqfs_xattr_writer_begin(xwr, 0); - if (ret) { - sqfs_perror(node->name, "recoding xattr key-value pairs\n", - ret); - return -1; - } - -#ifdef HAVE_SYS_XATTR_H - if (scan_xattr) { - path = get_full_path(path_prefix, node); - if (path == NULL) - return -1; - - ret = xattr_from_path(xwr, path); - free(path); - path = NULL; - if (ret) { - ret = -1; - goto out; - } - } -#else - (void)path_prefix; -#endif - - if (selinux_handle != NULL || xattr_map != NULL) { - path = fstree_get_path(node); - - if (path == NULL) { - perror("reconstructing absolute path"); - ret = -1; - goto out; - } - } - - if (xattr_map != NULL) { - ret = xattr_apply_map_file(path, xattr_map, xwr); - - if (ret) { - ret = -1; - goto out; - } - } - - if (selinux_handle != NULL) { - ret = selinux_relable_node(selinux_handle, xwr, node, path); - - if (ret) { - ret = -1; - goto out; - } - } - - if (sqfs_xattr_writer_end(xwr, &node->xattr_idx)) { - sqfs_perror(node->name, "completing xattr key-value pairs", - ret); - ret = -1; - goto out; - } - - if (S_ISDIR(node->mode)) { - node = node->data.children; - - while (node != NULL) { - if (xattr_xcan_dfs(path_prefix, selinux_handle, xwr, - scan_xattr, xattr_map, node)) { - ret = -1; - goto out; - } - - node = node->next; - } - } - -out: - free(path); - return ret; -} - -int xattrs_from_dir(fstree_t *fs, const char *path, void *selinux_handle, - void *xattr_map, sqfs_xattr_writer_t *xwr, bool scan_xattr) -{ - if (xwr == NULL) - return 0; - - if (selinux_handle == NULL && !scan_xattr && xattr_map == NULL) - return 0; - - return xattr_xcan_dfs(path, selinux_handle, xwr, scan_xattr, xattr_map, fs->root); -} diff --git a/bin/gensquashfs/src/mkfs.c b/bin/gensquashfs/src/mkfs.c index 39bc605..f5164ce 100644 --- a/bin/gensquashfs/src/mkfs.c +++ b/bin/gensquashfs/src/mkfs.c @@ -93,66 +93,6 @@ static int pack_files(sqfs_block_processor_t *data, fstree_t *fs, return 0; } -static int relabel_tree_dfs(const char *filename, sqfs_xattr_writer_t *xwr, - tree_node_t *n, void *selinux_handle, - void *xattrmap) -{ - char *path = fstree_get_path(n); - int ret; - - if (path == NULL) { - perror("getting absolute node path for SELinux relabeling"); - return -1; - } - - ret = sqfs_xattr_writer_begin(xwr, 0); - if (ret) { - sqfs_perror(filename, "recording xattr key-value pairs", ret); - return -1; - } - - if (xattrmap != NULL) - ret = xattr_apply_map_file(path, xattrmap, xwr); - - if (ret == 0 && selinux_handle != NULL) - ret = selinux_relable_node(selinux_handle, xwr, n, path); - - free(path); - if (ret == 0) - ret = sqfs_xattr_writer_end(xwr, &n->xattr_idx); - - if (ret) { - sqfs_perror(filename, "flushing completed key-value pairs", - ret); - return -1; - } - - if (S_ISDIR(n->mode)) { - for (n = n->data.children; n != NULL; n = n->next) { - if (relabel_tree_dfs(filename, xwr, n, - selinux_handle, xattrmap)) { - return -1; - } - } - } - - return 0; -} - -static int read_fstree(fstree_t *fs, options_t *opt, sqfs_xattr_writer_t *xwr, - void *selinux_handle, void *xattrmap) -{ - int ret; - - ret = fstree_from_file(fs, opt->infile, opt->packdir); - - if (ret == 0 && (selinux_handle != NULL || xattrmap != NULL)) - ret = relabel_tree_dfs(opt->cfg.filename, xwr, - fs->root, selinux_handle, xattrmap); - - return ret; -} - static void override_owner_dfs(const options_t *opt, tree_node_t *n) { if (opt->force_uid) @@ -219,7 +159,7 @@ int main(int argc, char **argv) if (ret != 0) goto out; } else { - if (read_fstree(&sqfs.fs, &opt, sqfs.xwr, sehnd, xattrmap)) + if (fstree_from_file(&sqfs.fs, opt.infile, opt.packdir)) goto out; } @@ -229,11 +169,9 @@ int main(int argc, char **argv) if (fstree_post_process(&sqfs.fs)) goto out; - if (opt.infile == NULL) { - if (xattrs_from_dir(&sqfs.fs, opt.packdir, sehnd, xattrmap, - sqfs.xwr, opt.scan_xattr)) { - goto out; - } + if (apply_xattrs(&sqfs.fs, opt.packdir, sehnd, xattrmap, + sqfs.xwr, opt.infile == NULL && opt.scan_xattr)) { + goto out; } if (sortfile != NULL) { diff --git a/bin/gensquashfs/src/mkfs.h b/bin/gensquashfs/src/mkfs.h index 5c5f02c..55ea2d5 100644 --- a/bin/gensquashfs/src/mkfs.h +++ b/bin/gensquashfs/src/mkfs.h @@ -74,8 +74,8 @@ struct XattrMap { void process_command_line(options_t *opt, int argc, char **argv); -int xattrs_from_dir(fstree_t *fs, const char *path, void *selinux_handle, - void *xattr_map, sqfs_xattr_writer_t *xwr, bool scan_xattr); +int apply_xattrs(fstree_t *fs, const char *path, void *selinux_handle, + void *xattr_map, sqfs_xattr_writer_t *xwr, bool scan_xattr); void *xattr_open_map_file(const char *path); -- cgit v1.2.3