From e3c8febe3b1e8c117d6299721814556d03e05805 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 10 Mar 2021 11:22:20 +0100 Subject: [PATCH] Refactor; common nomenclature, ordering, possib. security fix - Use _PATH_foo for all condition paths, *with* trailing / - Read condition file first, may not exist, in which case we save time - Change from libte makepath() to mkpath(), this changes from hard-coded 0777 perms on all cond dirs to 0755 -- possible security fix Signed-off-by: Joachim Wiberg --- src/cond-w.c | 21 +++++++++++---------- src/cond.c | 10 +++++----- src/cond.h | 9 ++++++--- src/initctl.c | 4 +--- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/cond-w.c b/src/cond-w.c index daa90d08..27120b19 100644 --- a/src/cond-w.c +++ b/src/cond-w.c @@ -99,13 +99,13 @@ static void cond_bump_reconf(void) unsigned int rgen; /* - * If %COND_RECONF does not exist, cond_get_gen() returns 0 + * If %_PATH_RECONF does not exist, cond_get_gen() returns 0 * meaning that rgen++ is always what we want. */ - rgen = cond_get_gen(COND_RECONF); + rgen = cond_get_gen(_PATH_RECONF); rgen++; - cond_set_gen(COND_RECONF, rgen); + cond_set_gen(_PATH_RECONF, rgen); } static int cond_checkpath(const char *path) @@ -119,7 +119,7 @@ static int cond_checkpath(const char *path) return 1; } - if (makepath(dir) && errno != EEXIST) { + if (mkpath(dir, 0755) && errno != EEXIST) { _pe("Failed creating dir '%s' for condition '%s'", dir, path); return 1; } @@ -134,7 +134,7 @@ int cond_set_path(const char *path, enum cond_state new) _d("%s", path); - rgen = cond_get_gen(COND_RECONF); + rgen = cond_get_gen(_PATH_RECONF); if (!rgen) { _e("Unable to read configuration generation (%s)", path); return -1; @@ -201,7 +201,8 @@ void cond_set_oneshot(const char *name) if (cond_checkpath(path)) return; - symlink(COND_RECONF, path); + if (symlink(_PATH_RECONF, path) && errno != EEXIST) + _pe("Failed creating onshot cond %s", name); cond_update(name); } @@ -234,13 +235,13 @@ static int reassert(const char *fpath, const struct stat *sb, int tflg, struct F if (tflg != FTW_F) return 0; - nm = strstr((char *)fpath, COND_DIR); + nm = strstr((char *)fpath, COND_BASE); if (!nm) { _e("Incorrect condtion path %s, cannot reassert", fpath); return 1; } - nm += sizeof(COND_DIR); + nm += strlen(COND_BASE); _d("Reasserting %s => %s", fpath, nm); cond_set(nm); @@ -261,8 +262,8 @@ void cond_init(void) { char path[MAX_ARG_LEN]; - if (makepath(pid_runpath(COND_PATH, path, sizeof(path))) && errno != EEXIST) { - _pe("Failed creating condition base directory '%s'", COND_PATH); + if (mkpath(pid_runpath(_PATH_COND, path, sizeof(path)), 0755) && errno != EEXIST) { + _pe("Failed creating condition base directory '%s'", _PATH_COND); return; } diff --git a/src/cond.c b/src/cond.c index c8baf08f..7e609aa7 100644 --- a/src/cond.c +++ b/src/cond.c @@ -45,7 +45,7 @@ const char *cond_path(const char *name) static char path[256]; char tmp[MAX_ARG_LEN]; - snprintf(tmp, sizeof(tmp), COND_PATH "/%s", name); + snprintf(tmp, sizeof(tmp), _PATH_COND "%s", name); return pid_runpath(tmp, path, sizeof(path)); } @@ -74,14 +74,14 @@ enum cond_state cond_get_path(const char *path) { int cgen, rgen; - rgen = cond_get_gen(COND_RECONF); - if (!rgen) - return COND_OFF; - cgen = cond_get_gen(path); if (!cgen) return COND_OFF; + rgen = cond_get_gen(_PATH_RECONF); + if (!rgen) + return COND_OFF; + return (cgen == rgen) ? COND_ON : COND_FLUX; } diff --git a/src/cond.h b/src/cond.h index 5ff5acbb..ab22cce2 100644 --- a/src/cond.h +++ b/src/cond.h @@ -4,9 +4,12 @@ #include #include -#define COND_DIR "finit/cond" -#define COND_PATH _PATH_VARRUN COND_DIR -#define COND_RECONF COND_PATH "/reconf" +#define COND_BASE "finit/cond" +#define COND_USR "usr/" + +#define _PATH_COND _PATH_VARRUN COND_BASE "/" +#define _PATH_CONDUSR _PATH_COND COND_USR +#define _PATH_RECONF _PATH_COND "reconf" typedef enum cond_state { COND_OFF = 0, diff --git a/src/initctl.c b/src/initctl.c index 3e797a42..99f72110 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -42,8 +42,6 @@ #include "service.h" #include "util.h" -#define _PATH_COND _PATH_VARRUN "finit/cond/" - #define NONE " " #define PIPE plain ? "|" : "│" #define FORK plain ? "|-" : "├─" @@ -256,7 +254,7 @@ static int dump_one_cond(const char *fpath, const struct stat *sb, int tflag, st if (tflag != FTW_F) return 0; - if (!strcmp(fpath, _PATH_COND "reconf")) + if (!strcmp(fpath, _PATH_RECONF)) return 0; asserted = condstr(cond_get_path(fpath));