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 <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2021-03-11 11:44:23 +01:00
1 parent 32ca117949
commit e3c8febe3b
4 files changed
+23 -21

No files matched your search

+11 -10
View File
@@ -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;
}
+5 -5
View File
@@ -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;
}
+6 -3
View File
@@ -4,9 +4,12 @@
#include <paths.h>
#include <svc.h>
#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,
+1 -3
View File
@@ -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));