util-lib: get_current_dir_name() can return errors other than ENOMEM

get_current_dir_name() can return a variety of errors, not just ENOMEM,
hence don't blindly turn its errors to ENOMEM, but return correct errors
in path_make_absolute_cwd().

This trickles down into a couple of other functions, some of which
receive unrelated minor fixes too with this commit.
This commit is contained in:
Lennart Poettering 2015-10-22 19:28:31 +02:00
parent 85eca92e20
commit 0f47436510
10 changed files with 139 additions and 124 deletions

View file

@ -84,20 +84,25 @@ int path_get_parent(const char *path, char **_r) {
return 0; return 0;
} }
char **path_split_and_make_absolute(const char *p) { int path_split_and_make_absolute(const char *p, char ***ret) {
char **l; char **l;
int r;
assert(p); assert(p);
assert(ret);
l = strv_split(p, ":"); l = strv_split(p, ":");
if (!l) if (!l)
return NULL; return NULL;
if (!path_strv_make_absolute_cwd(l)) { r = path_strv_make_absolute_cwd(l);
if (r < 0) {
strv_free(l); strv_free(l);
return NULL; return r;
} }
return l; *ret = l;
return r;
} }
char *path_make_absolute(const char *p, const char *prefix) { char *path_make_absolute(const char *p, const char *prefix) {
@ -112,22 +117,31 @@ char *path_make_absolute(const char *p, const char *prefix) {
return strjoin(prefix, "/", p, NULL); return strjoin(prefix, "/", p, NULL);
} }
char *path_make_absolute_cwd(const char *p) { int path_make_absolute_cwd(const char *p, char **ret) {
_cleanup_free_ char *cwd = NULL; char *c;
assert(p); assert(p);
assert(ret);
/* Similar to path_make_absolute(), but prefixes with the /* Similar to path_make_absolute(), but prefixes with the
* current working directory. */ * current working directory. */
if (path_is_absolute(p)) if (path_is_absolute(p))
return strdup(p); c = strdup(p);
else {
_cleanup_free_ char *cwd = NULL;
cwd = get_current_dir_name(); cwd = get_current_dir_name();
if (!cwd) if (!cwd)
return NULL; return -errno;
return strjoin(cwd, "/", p, NULL); c = strjoin(cwd, "/", p, NULL);
}
if (!c)
return -ENOMEM;
*ret = c;
return 0;
} }
int path_make_relative(const char *from_dir, const char *to_path, char **_r) { int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
@ -215,8 +229,9 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
return 0; return 0;
} }
char **path_strv_make_absolute_cwd(char **l) { int path_strv_make_absolute_cwd(char **l) {
char **s; char **s;
int r;
/* Goes through every item in the string list and makes it /* Goes through every item in the string list and makes it
* absolute. This works in place and won't rollback any * absolute. This works in place and won't rollback any
@ -225,15 +240,15 @@ char **path_strv_make_absolute_cwd(char **l) {
STRV_FOREACH(s, l) { STRV_FOREACH(s, l) {
char *t; char *t;
t = path_make_absolute_cwd(*s); r = path_make_absolute_cwd(*s, &t);
if (!t) if (r < 0)
return NULL; return r;
free(*s); free(*s);
*s = t; *s = t;
} }
return l; return 0;
} }
char **path_strv_resolve(char **l, const char *prefix) { char **path_strv_resolve(char **l, const char *prefix) {
@ -719,13 +734,9 @@ int find_binary(const char *name, char **ret) {
return -errno; return -errno;
if (ret) { if (ret) {
char *rs; r = path_make_absolute_cwd(name, ret);
if (r < 0)
rs = path_make_absolute_cwd(name); return r;
if (!rs)
return -ENOMEM;
*ret = rs;
} }
return 0; return 0;
@ -750,6 +761,9 @@ int find_binary(const char *name, char **ret) {
if (r == 0) if (r == 0)
break; break;
if (!path_is_absolute(element))
continue;
j = strjoin(element, "/", name, NULL); j = strjoin(element, "/", name, NULL);
if (!j) if (!j)
return -ENOMEM; return -ENOMEM;

View file

@ -36,11 +36,11 @@
#endif #endif
bool is_path(const char *p) _pure_; bool is_path(const char *p) _pure_;
char** path_split_and_make_absolute(const char *p); int path_split_and_make_absolute(const char *p, char ***ret);
int path_get_parent(const char *path, char **parent); int path_get_parent(const char *path, char **parent);
bool path_is_absolute(const char *p) _pure_; bool path_is_absolute(const char *p) _pure_;
char* path_make_absolute(const char *p, const char *prefix); char* path_make_absolute(const char *p, const char *prefix);
char* path_make_absolute_cwd(const char *p); int path_make_absolute_cwd(const char *p, char **ret);
int path_make_relative(const char *from_dir, const char *to_path, char **_r); int path_make_relative(const char *from_dir, const char *to_path, char **_r);
char* path_kill_slashes(char *path); char* path_kill_slashes(char *path);
char* path_startswith(const char *path, const char *prefix) _pure_; char* path_startswith(const char *path, const char *prefix) _pure_;
@ -49,7 +49,7 @@ bool path_equal(const char *a, const char *b) _pure_;
bool path_equal_or_files_same(const char *a, const char *b); bool path_equal_or_files_same(const char *a, const char *b);
char* path_join(const char *root, const char *path, const char *rest); char* path_join(const char *root, const char *path, const char *rest);
char** path_strv_make_absolute_cwd(char **l); int path_strv_make_absolute_cwd(char **l);
char** path_strv_resolve(char **l, const char *prefix); char** path_strv_resolve(char **l, const char *prefix);
char** path_strv_resolve_uniq(char **l, const char *prefix); char** path_strv_resolve_uniq(char **l, const char *prefix);

View file

@ -171,15 +171,15 @@ int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
int mac_selinux_apply(const char *path, const char *label) { int mac_selinux_apply(const char *path, const char *label) {
#ifdef HAVE_SELINUX #ifdef HAVE_SELINUX
assert(path);
assert(label);
if (!mac_selinux_use()) if (!mac_selinux_use())
return 0; return 0;
assert(path);
assert(label);
if (setfilecon(path, (security_context_t) label) < 0) { if (setfilecon(path, (security_context_t) label) < 0) {
log_enforcing("Failed to set SELinux security context %s on path %s: %m", label, path); log_enforcing("Failed to set SELinux security context %s on path %s: %m", label, path);
if (security_getenforce() == 1) if (security_getenforce() > 0)
return -errno; return -errno;
} }
#endif #endif
@ -312,10 +312,10 @@ char* mac_selinux_free(char *label) {
} }
int mac_selinux_create_file_prepare(const char *path, mode_t mode) { int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
int r = 0;
#ifdef HAVE_SELINUX #ifdef HAVE_SELINUX
_cleanup_security_context_free_ security_context_t filecon = NULL; _cleanup_security_context_free_ security_context_t filecon = NULL;
int r;
assert(path); assert(path);
@ -325,34 +325,33 @@ int mac_selinux_create_file_prepare(const char *path, mode_t mode) {
if (path_is_absolute(path)) if (path_is_absolute(path))
r = selabel_lookup_raw(label_hnd, &filecon, path, mode); r = selabel_lookup_raw(label_hnd, &filecon, path, mode);
else { else {
_cleanup_free_ char *newpath; _cleanup_free_ char *newpath = NULL;
newpath = path_make_absolute_cwd(path); r = path_make_absolute_cwd(path, &newpath);
if (!newpath) if (r < 0)
return -ENOMEM; return r;
r = selabel_lookup_raw(label_hnd, &filecon, newpath, mode); r = selabel_lookup_raw(label_hnd, &filecon, newpath, mode);
} }
/* No context specified by the policy? Proceed without setting it. */ if (r < 0) {
if (r < 0 && errno == ENOENT) /* No context specified by the policy? Proceed without setting it. */
return 0; if (errno == ENOENT)
return 0;
if (r < 0) log_enforcing("Failed to determine SELinux security context for %s: %m", path);
r = -errno; } else {
else { if (setfscreatecon(filecon) >= 0)
r = setfscreatecon(filecon); return 0; /* Success! */
if (r < 0) {
log_enforcing("Failed to set SELinux security context %s for %s: %m", filecon, path); log_enforcing("Failed to set SELinux security context %s for %s: %m", filecon, path);
r = -errno;
}
} }
if (r < 0 && security_getenforce() == 0) if (security_getenforce() > 0)
r = 0; return -errno;
#endif
return r; #endif
return 0;
} }
void mac_selinux_create_file_clear(void) { void mac_selinux_create_file_clear(void) {
@ -405,6 +404,7 @@ int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
#ifdef HAVE_SELINUX #ifdef HAVE_SELINUX
_cleanup_security_context_free_ security_context_t fcon = NULL; _cleanup_security_context_free_ security_context_t fcon = NULL;
const struct sockaddr_un *un; const struct sockaddr_un *un;
bool context_changed = false;
char *path; char *path;
int r; int r;
@ -420,7 +420,7 @@ int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
goto skipped; goto skipped;
/* Filter out anonymous sockets */ /* Filter out anonymous sockets */
if (addrlen < sizeof(sa_family_t) + 1) if (addrlen < offsetof(struct sockaddr_un, sun_path) + 1)
goto skipped; goto skipped;
/* Filter out abstract namespace sockets */ /* Filter out abstract namespace sockets */
@ -433,36 +433,44 @@ int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
if (path_is_absolute(path)) if (path_is_absolute(path))
r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK); r = selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK);
else { else {
_cleanup_free_ char *newpath; _cleanup_free_ char *newpath = NULL;
newpath = path_make_absolute_cwd(path); r = path_make_absolute_cwd(path, &newpath);
if (!newpath) if (r < 0)
return -ENOMEM; return r;
r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK); r = selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK);
} }
if (r == 0) if (r < 0) {
r = setfscreatecon(fcon); /* No context specified by the policy? Proceed without setting it */
if (errno == ENOENT)
goto skipped;
if (r < 0 && errno != ENOENT) { log_enforcing("Failed to determine SELinux security context for %s: %m", path);
log_enforcing("Failed to set SELinux security context %s for %s: %m", fcon, path); if (security_getenforce() > 0)
return -errno;
if (security_getenforce() == 1) { } else {
r = -errno; if (setfscreatecon(fcon) < 0) {
goto finish; log_enforcing("Failed to set SELinux security context %s for %s: %m", fcon, path);
} if (security_getenforce() > 0)
return -errno;
} else
context_changed = true;
} }
r = bind(fd, addr, addrlen); r = bind(fd, addr, addrlen) < 0 ? -errno : 0;
if (r < 0)
r = -errno; if (context_changed)
setfscreatecon(NULL);
finish:
setfscreatecon(NULL);
return r; return r;
skipped: skipped:
#endif #endif
return bind(fd, addr, addrlen) < 0 ? -errno : 0; if (bind(fd, addr, addrlen) < 0)
return -errno;
return 0;
} }

View file

@ -690,16 +690,12 @@ static int parse_argv(int argc, char *argv[]) {
return version(); return version();
case ARG_ROOT: case ARG_ROOT:
free(arg_root); arg_root = mfree(arg_root);
arg_root = path_make_absolute_cwd(optarg); r = path_make_absolute_cwd(optarg, &arg_root);
if (!arg_root) if (r < 0)
return log_oom(); return log_error_errno(r, "Failed to make root path absolute: %m");
path_kill_slashes(arg_root); path_kill_slashes(arg_root);
if (path_equal(arg_root, "/"))
arg_root = mfree(arg_root);
break; break;
case ARG_LOCALE: case ARG_LOCALE:

View file

@ -84,37 +84,35 @@ static Set *new_matches(void) {
} }
static int add_match(Set *set, const char *match) { static int add_match(Set *set, const char *match) {
int r = -ENOMEM;
unsigned pid;
const char* prefix;
char *pattern = NULL;
_cleanup_free_ char *p = NULL; _cleanup_free_ char *p = NULL;
char *pattern = NULL;
const char* prefix;
pid_t pid;
int r;
if (strchr(match, '=')) if (strchr(match, '='))
prefix = ""; prefix = "";
else if (strchr(match, '/')) { else if (strchr(match, '/')) {
p = path_make_absolute_cwd(match); r = path_make_absolute_cwd(match, &p);
if (!p) if (r < 0)
goto fail; goto fail;
match = p; match = p;
prefix = "COREDUMP_EXE="; prefix = "COREDUMP_EXE=";
} } else if (parse_pid(match, &pid) >= 0)
else if (safe_atou(match, &pid) == 0)
prefix = "COREDUMP_PID="; prefix = "COREDUMP_PID=";
else else
prefix = "COREDUMP_COMM="; prefix = "COREDUMP_COMM=";
pattern = strjoin(prefix, match, NULL); pattern = strjoin(prefix, match, NULL);
if (!pattern) if (!pattern) {
r = -ENOMEM;
goto fail; goto fail;
}
log_debug("Adding pattern: %s", pattern); log_debug("Adding pattern: %s", pattern);
r = set_consume(set, pattern); r = set_consume(set, pattern);
if (r < 0) { if (r < 0)
log_error_errno(r, "Failed to add pattern: %m");
goto fail; goto fail;
}
return 0; return 0;
fail: fail:

View file

@ -1092,9 +1092,10 @@ static int copy_files(int argc, char *argv[], void *userdata) {
container_path = copy_from ? argv[2] : dest; container_path = copy_from ? argv[2] : dest;
if (!path_is_absolute(host_path)) { if (!path_is_absolute(host_path)) {
abs_host_path = path_make_absolute_cwd(host_path); r = path_make_absolute_cwd(host_path, &abs_host_path);
if (!abs_host_path) if (r < 0)
return log_oom(); return log_error_errno(r, "Failed to make path absolute: %m");
host_path = abs_host_path; host_path = abs_host_path;
} }
@ -1110,10 +1111,8 @@ static int copy_files(int argc, char *argv[], void *userdata) {
argv[1], argv[1],
copy_from ? container_path : host_path, copy_from ? container_path : host_path,
copy_from ? host_path : container_path); copy_from ? host_path : container_path);
if (r < 0) { if (r < 0)
log_error("Failed to copy: %s", bus_error_message(&error, -r)); return log_error_errno(r, "Failed to copy: %s", bus_error_message(&error, r));
return r;
}
return 0; return 0;
} }

View file

@ -278,6 +278,7 @@ static int custom_mounts_prepare(void) {
static int set_sanitized_path(char **b, const char *path) { static int set_sanitized_path(char **b, const char *path) {
char *p; char *p;
int r;
assert(b); assert(b);
assert(path); assert(path);
@ -287,9 +288,9 @@ static int set_sanitized_path(char **b, const char *path) {
if (errno != ENOENT) if (errno != ENOENT)
return -errno; return -errno;
p = path_make_absolute_cwd(path); r = path_make_absolute_cwd(path, &p);
if (!p) if (r < 0)
return -ENOMEM; return r;
} }
free(*b); free(*b);

View file

@ -210,7 +210,7 @@ static char** user_dirs(
if (strv_extend(&res, generator_late) < 0) if (strv_extend(&res, generator_late) < 0)
return NULL; return NULL;
if (!path_strv_make_absolute_cwd(res)) if (path_strv_make_absolute_cwd(res) < 0)
return NULL; return NULL;
tmp = res; tmp = res;
@ -244,6 +244,7 @@ int lookup_paths_init(
const char *e; const char *e;
bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */ bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */
int r;
assert(p); assert(p);
@ -259,9 +260,9 @@ int lookup_paths_init(
/* FIXME: empty components in other places should be /* FIXME: empty components in other places should be
* rejected. */ * rejected. */
p->unit_path = path_split_and_make_absolute(e); r = path_split_and_make_absolute(e, &p->unit_path);
if (!p->unit_path) if (r < 0)
return -ENOMEM; return r;
} else } else
p->unit_path = NULL; p->unit_path = NULL;
@ -269,7 +270,6 @@ int lookup_paths_init(
/* Let's figure something out. */ /* Let's figure something out. */
_cleanup_strv_free_ char **unit_path; _cleanup_strv_free_ char **unit_path;
int r;
/* For the user units we include share/ in the search /* For the user units we include share/ in the search
* path in order to comply with the XDG basedir spec. * path in order to comply with the XDG basedir spec.
@ -342,9 +342,9 @@ int lookup_paths_init(
e = getenv("SYSTEMD_SYSVINIT_PATH"); e = getenv("SYSTEMD_SYSVINIT_PATH");
if (e) { if (e) {
p->sysvinit_path = path_split_and_make_absolute(e); r = path_split_and_make_absolute(e, &p->sysvinit_path);
if (!p->sysvinit_path) if (r < 0)
return -ENOMEM; return r;
} else } else
p->sysvinit_path = NULL; p->sysvinit_path = NULL;
@ -360,9 +360,9 @@ int lookup_paths_init(
e = getenv("SYSTEMD_SYSVRCND_PATH"); e = getenv("SYSTEMD_SYSVRCND_PATH");
if (e) { if (e) {
p->sysvrcnd_path = path_split_and_make_absolute(e); r = path_split_and_make_absolute(e, &p->sysvrcnd_path);
if (!p->sysvrcnd_path) if (r < 0)
return -ENOMEM; return r;
} else } else
p->sysvrcnd_path = NULL; p->sysvrcnd_path = NULL;
@ -417,9 +417,8 @@ void lookup_paths_free(LookupPaths *p) {
p->unit_path = strv_free(p->unit_path); p->unit_path = strv_free(p->unit_path);
#ifdef HAVE_SYSV_COMPAT #ifdef HAVE_SYSV_COMPAT
strv_free(p->sysvinit_path); p->sysvinit_path = strv_free(p->sysvinit_path);
strv_free(p->sysvrcnd_path); p->sysvrcnd_path = strv_free(p->sysvrcnd_path);
p->sysvinit_path = p->sysvrcnd_path = NULL;
#endif #endif
} }

View file

@ -1762,7 +1762,7 @@ static int parse_argv(int argc, char *argv[]) {
{} {}
}; };
int c; int c, r;
assert(argc >= 0); assert(argc >= 0);
assert(argv); assert(argv);
@ -1779,10 +1779,10 @@ static int parse_argv(int argc, char *argv[]) {
return version(); return version();
case ARG_ROOT: case ARG_ROOT:
free(arg_root); arg_root = mfree(arg_root);
arg_root = path_make_absolute_cwd(optarg); r = path_make_absolute_cwd(optarg, &arg_root);
if (!arg_root) if (r < 0)
return log_oom(); return log_error_errno(r, "Failed to make root path absolute: %m");
path_kill_slashes(arg_root); path_kill_slashes(arg_root);
break; break;

View file

@ -2101,7 +2101,7 @@ static int parse_argv(int argc, char *argv[]) {
{} {}
}; };
int c; int c, r;
assert(argc >= 0); assert(argc >= 0);
assert(argv); assert(argv);
@ -2144,10 +2144,10 @@ static int parse_argv(int argc, char *argv[]) {
break; break;
case ARG_ROOT: case ARG_ROOT:
free(arg_root); arg_root = mfree(arg_root);
arg_root = path_make_absolute_cwd(optarg); r = path_make_absolute_cwd(optarg, &arg_root);
if (!arg_root) if (r < 0)
return log_oom(); return log_error_errno(r, "Failed to make root path absolute: %m");
path_kill_slashes(arg_root); path_kill_slashes(arg_root);
break; break;