aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/fstree.h8
-rw-r--r--lib/Makemodule.am3
-rw-r--r--lib/fstree/get_path.c36
-rw-r--r--lib/fstree/selinux.c45
4 files changed, 50 insertions, 42 deletions
diff --git a/include/fstree.h b/include/fstree.h
index 284792b..ef2ffb1 100644
--- a/include/fstree.h
+++ b/include/fstree.h
@@ -254,4 +254,12 @@ int fstree_relabel_selinux(fstree_t *fs, const char *filename);
/* Returns 0 on success. Prints to stderr on failure */
int fstree_gen_inode_table(fstree_t *fs);
+/*
+ Generate a string holding the full path of a node. Returned
+ string must be freed.
+
+ Returns NULL on failure and sets errno.
+*/
+char *fstree_get_path(tree_node_t *node);
+
#endif /* FSTREE_H */
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
index bf4ed61..618fbf2 100644
--- a/lib/Makemodule.am
+++ b/lib/Makemodule.am
@@ -1,6 +1,7 @@
libfstree_a_SOURCES = lib/fstree/fstree.c lib/fstree/fstree_from_file.c
libfstree_a_SOURCES += lib/fstree/fstree_sort.c lib/fstree/fstree_from_dir.c
-libfstree_a_SOURCES += lib/fstree/gen_inode_table.c include/fstree.h
+libfstree_a_SOURCES += lib/fstree/gen_inode_table.c lib/fstree/get_path.c
+libfstree_a_SOURCES += include/fstree.h
libfstree_a_CFLAGS = $(AM_CFLAGS)
libfstree_a_CPPFLAGS = $(AM_CPPFLAGS)
diff --git a/lib/fstree/get_path.c b/lib/fstree/get_path.c
new file mode 100644
index 0000000..4b6f61e
--- /dev/null
+++ b/lib/fstree/get_path.c
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <string.h>
+#include <stdlib.h>
+
+char *fstree_get_path(tree_node_t *node)
+{
+ tree_node_t *it;
+ char *str, *ptr;
+ size_t len = 0;
+
+ if (node->parent == NULL)
+ return strdup("/");
+
+ for (it = node; it != NULL && it->parent != NULL; it = it->parent) {
+ len += strlen(it->name) + 1;
+ }
+
+ str = malloc(len + 1);
+ if (str == NULL)
+ return NULL;
+
+ ptr = str + len;
+ *ptr = '\0';
+
+ for (it = node; it != NULL && it->parent != NULL; it = it->parent) {
+ len = strlen(it->name);
+ ptr -= len;
+
+ memcpy(ptr, it->name, len);
+ *(--ptr) = '/';
+ }
+
+ return str;
+}
diff --git a/lib/fstree/selinux.c b/lib/fstree/selinux.c
index ef858f2..ec14a93 100644
--- a/lib/fstree/selinux.c
+++ b/lib/fstree/selinux.c
@@ -10,45 +10,6 @@
#define XATTR_NAME_SELINUX "security.selinux"
#define XATTR_VALUE_SELINUX "system_u:object_r:unlabeled_t:s0"
-static char *get_path(tree_node_t *node)
-{
- tree_node_t *it;
- char *str, *ptr;
- size_t len = 0;
-
- if (node->parent == NULL) {
- str = strdup("/");
-
- if (str == NULL)
- goto fail_alloc;
- return str;
- }
-
- for (it = node; it != NULL && it->parent != NULL; it = it->parent) {
- len += strlen(it->name) + 1;
- }
-
- str = malloc(len + 1);
- if (str == NULL)
- goto fail_alloc;
-
- ptr = str + len;
- *ptr = '\0';
-
- for (it = node; it != NULL && it->parent != NULL; it = it->parent) {
- len = strlen(it->name);
- ptr -= len;
-
- memcpy(ptr, it->name, len);
- *(--ptr) = '/';
- }
-
- return str;
-fail_alloc:
- perror("relabeling files");
- return NULL;
-}
-
static int relable_node(fstree_t *fs, struct selabel_handle *sehnd,
tree_node_t *node)
{
@@ -56,9 +17,11 @@ static int relable_node(fstree_t *fs, struct selabel_handle *sehnd,
tree_node_t *it;
int ret;
- path = get_path(node);
- if (path == NULL)
+ path = fstree_get_path(node);
+ if (path == NULL) {
+ perror("relabeling files");
return -1;
+ }
if (selabel_lookup(sehnd, &context, path, node->mode) < 0) {
free(path);