2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2014-06-24 13:30:11 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdbool.h>
|
2014-06-24 13:30:11 +02:00
|
|
|
#include <stdlib.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <sys/stat.h>
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <syslog.h>
|
2014-06-24 13:30:11 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-26 23:20:41 +01:00
|
|
|
#include "base-filesystem.h"
|
|
|
|
#include "fd-util.h"
|
2014-06-24 13:30:11 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "macro.h"
|
2019-03-14 13:14:33 +01:00
|
|
|
#include "nulstr-util.h"
|
2019-04-03 16:53:14 +02:00
|
|
|
#include "path-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-10-26 23:20:41 +01:00
|
|
|
#include "umask-util.h"
|
2015-10-27 00:42:07 +01:00
|
|
|
#include "user-util.h"
|
2014-06-24 13:30:11 +02:00
|
|
|
|
|
|
|
typedef struct BaseFilesystem {
|
|
|
|
const char *dir;
|
|
|
|
mode_t mode;
|
|
|
|
const char *target;
|
2014-07-01 12:12:40 +02:00
|
|
|
const char *exists;
|
2015-09-30 15:50:42 +02:00
|
|
|
bool ignore_failure;
|
2014-06-24 13:30:11 +02:00
|
|
|
} BaseFilesystem;
|
|
|
|
|
|
|
|
static const BaseFilesystem table[] = {
|
2014-07-07 17:45:53 +02:00
|
|
|
{ "bin", 0, "usr/bin\0", NULL },
|
|
|
|
{ "lib", 0, "usr/lib\0", NULL },
|
2015-09-30 15:50:42 +02:00
|
|
|
{ "root", 0755, NULL, NULL, true },
|
2014-07-07 17:45:53 +02:00
|
|
|
{ "sbin", 0, "usr/sbin\0", NULL },
|
2015-05-21 16:30:58 +02:00
|
|
|
{ "usr", 0755, NULL, NULL },
|
|
|
|
{ "var", 0755, NULL, NULL },
|
|
|
|
{ "etc", 0755, NULL, NULL },
|
2017-03-05 21:19:29 +01:00
|
|
|
{ "proc", 0755, NULL, NULL, true },
|
|
|
|
{ "sys", 0755, NULL, NULL, true },
|
|
|
|
{ "dev", 0755, NULL, NULL, true },
|
2014-07-01 11:42:58 +02:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
2014-07-07 17:45:53 +02:00
|
|
|
{ "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
|
|
|
|
"usr/lib64\0", "ld-linux-x86-64.so.2" },
|
2014-07-01 11:42:58 +02:00
|
|
|
#endif
|
2014-06-24 13:30:11 +02:00
|
|
|
};
|
|
|
|
|
2015-05-21 16:30:58 +02:00
|
|
|
int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
|
2014-06-24 13:30:11 +02:00
|
|
|
_cleanup_close_ int fd = -1;
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
size_t i;
|
2019-04-03 16:52:19 +02:00
|
|
|
int r;
|
2014-06-24 13:30:11 +02:00
|
|
|
|
|
|
|
fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (fd < 0)
|
|
|
|
return log_error_errno(errno, "Failed to open root file system: %m");
|
2014-06-24 13:30:11 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ELEMENTSOF(table); i ++) {
|
2014-09-03 13:22:40 +02:00
|
|
|
if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
|
|
|
|
continue;
|
|
|
|
|
2014-06-24 13:30:11 +02:00
|
|
|
if (table[i].target) {
|
2014-09-03 19:00:21 +02:00
|
|
|
const char *target = NULL, *s;
|
2014-07-01 11:42:58 +02:00
|
|
|
|
|
|
|
/* check if one of the targets exists */
|
|
|
|
NULSTR_FOREACH(s, table[i].target) {
|
|
|
|
if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
|
|
|
|
continue;
|
|
|
|
|
2014-07-01 12:12:40 +02:00
|
|
|
/* check if a specific file exists at the target path */
|
|
|
|
if (table[i].exists) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
2019-04-03 16:53:14 +02:00
|
|
|
p = path_join(s, table[i].exists);
|
2014-07-01 12:12:40 +02:00
|
|
|
if (!p)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-01 11:42:58 +02:00
|
|
|
target = s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target)
|
2014-06-24 13:30:11 +02:00
|
|
|
continue;
|
|
|
|
|
2019-04-03 16:55:01 +02:00
|
|
|
if (symlinkat(target, fd, table[i].dir) < 0) {
|
|
|
|
log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
|
|
|
|
"Failed to create symlink at %s/%s: %m", root, table[i].dir);
|
|
|
|
|
|
|
|
if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return -errno;
|
|
|
|
}
|
2015-05-21 16:30:58 +02:00
|
|
|
|
2016-12-08 10:50:29 +01:00
|
|
|
if (uid_is_valid(uid) || gid_is_valid(gid)) {
|
2015-05-21 16:30:58 +02:00
|
|
|
if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
|
|
|
|
}
|
|
|
|
|
2014-06-24 13:30:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RUN_WITH_UMASK(0000)
|
|
|
|
r = mkdirat(fd, table[i].dir, table[i].mode);
|
2019-04-03 16:55:01 +02:00
|
|
|
if (r < 0) {
|
|
|
|
log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
|
2015-09-30 15:50:42 +02:00
|
|
|
"Failed to create directory at %s/%s: %m", root, table[i].dir);
|
|
|
|
|
2019-04-03 16:55:01 +02:00
|
|
|
if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
|
|
|
|
continue;
|
2017-03-16 03:30:15 +01:00
|
|
|
|
2019-04-03 16:55:01 +02:00
|
|
|
return -errno;
|
2015-09-30 15:50:42 +02:00
|
|
|
}
|
2015-05-21 16:30:58 +02:00
|
|
|
|
|
|
|
if (uid != UID_INVALID || gid != UID_INVALID) {
|
|
|
|
if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
|
|
|
|
}
|
2014-06-24 13:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|