X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=21edd3ac2f38ab47e5c3453e626f4171ba557d72;hp=30512d16462bf5c528660a66a9ab8e10380ed402;hb=c50e4f95d8cfcd21bde2b0d1ff24b4de8fef4976;hpb=87b0284327e34a4b96c22085fa2cdb3219294991 diff --git a/src/shared/util.c b/src/shared/util.c index 30512d164..21edd3ac2 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -2337,7 +2337,7 @@ void rename_process(const char name[8]) { if (!saved_argv[i]) break; - memset(saved_argv[i], 0, strlen(saved_argv[i])); + memzero(saved_argv[i], strlen(saved_argv[i])); } } } @@ -5792,7 +5792,7 @@ void* greedy_realloc0(void **p, size_t *allocated, size_t need) { return NULL; if (*allocated > prev) - memset(&q[prev], 0, *allocated - prev); + memzero(&q[prev], *allocated - prev); return q; } @@ -5838,20 +5838,6 @@ bool id128_is_valid(const char *s) { return true; } -void parse_user_at_host(char *arg, char **user, char **host) { - assert(arg); - assert(user); - assert(host); - - *host = strchr(arg, '@'); - if (*host == NULL) - *host = arg; - else { - *host[0]++ = '\0'; - *user = arg; - } -} - int split_pair(const char *s, const char *sep, char **l, char **r) { char *x, *a, *b; @@ -6093,43 +6079,20 @@ int getpeersec(int fd, char **ret) { return 0; } +/* This is much like like mkostemp() but is subject to umask(). */ int mkostemp_safe(char *pattern, int flags) { - unsigned long tries = TMP_MAX; - char *s; - int r; _cleanup_umask_ mode_t u; + int fd; assert(pattern); u = umask(077); - /* This is much like like mkostemp() but avoids using any - * static variables, thus is async signal safe. Also, it's not - * subject to umask(). */ - - s = endswith(pattern, "XXXXXX"); - if (!s) - return -EINVAL; - - while (tries--) { - unsigned i; - int fd; - - r = dev_urandom(s, 6); - if (r < 0) - return r; - - for (i = 0; i < 6; i++) - s[i] = ALPHANUMERICAL[(unsigned) s[i] % (sizeof(ALPHANUMERICAL)-1)]; - - fd = open(pattern, flags|O_EXCL|O_CREAT|O_NOCTTY|O_NOFOLLOW, S_IRUSR|S_IWUSR); - if (fd >= 0) - return fd; - if (!IN_SET(errno, EEXIST, EINTR)) - return -errno; - } + fd = mkostemp(pattern, flags); + if (fd < 0) + return -errno; - return -EEXIST; + return fd; } int open_tmpfile(const char *path, int flags) { @@ -6155,3 +6118,21 @@ int open_tmpfile(const char *path, int flags) { unlink(p); return fd; } + +int fd_warn_permissions(const char *path, int fd) { + struct stat st; + + if (fstat(fd, &st) < 0) + return -errno; + + if (st.st_mode & 0111) + log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path); + + if (st.st_mode & 0002) + log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path); + + if (getpid() == 1 && (st.st_mode & 0044) != 0044) + log_warning("Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.", path); + + return 0; +}