10/14
This commit is contained in:
1 parent
ee4bcc3b77
commit
ae54f74070
53 files changed
+8916
-1424
No files matched your search
+149
-42
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/limits.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -13,13 +14,6 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *path;
|
||||
mode_t mode;
|
||||
}
|
||||
dir_t;
|
||||
|
||||
/*
|
||||
* On-disk layout. Each scope keeps everything it ever creates, renames
|
||||
* or locks under ONE base directory it owns:
|
||||
@@ -35,6 +29,21 @@ dir_t;
|
||||
* an unprivileged user run their own transactions at all.
|
||||
*/
|
||||
#define GL_USR_ROOT "/glacier/usr"
|
||||
|
||||
/* In a package's version dir: "installed as a dependency, not by name".
|
||||
* Dot-prefixed like the other store markers, so seeding carries it and
|
||||
* linking skips it; the index's 4th field is derived from it. */
|
||||
#define GL_AUTO_MARKER ".glacier-auto"
|
||||
/* The other files glacier keeps in a version dir (see gl_install_pkg):
|
||||
* the package's saved manifest, and the one-shot markers for the next
|
||||
* link pass (this transaction installed it; it may displace, -O). A
|
||||
* package may not ship any top-level name starting with GL_MARKER_PREFIX:
|
||||
* these are glacier's, and one shipped as a symlink would redirect
|
||||
* glacier's own writes. */
|
||||
#define GL_MARKER_PREFIX ".glacier-"
|
||||
#define GL_MANIFEST_COPY ".glacier-manifest"
|
||||
#define GL_FRESH_MARKER ".glacier-fresh"
|
||||
#define GL_OVERWRITE_MARKER ".glacier-overwrite"
|
||||
#define GL_SYS_ROOT "/glacier/sys"
|
||||
|
||||
static inline void
|
||||
@@ -78,6 +87,98 @@ gl_usr_base_check(uid_t uid)
|
||||
return GL_USR_BASE_OK;
|
||||
}
|
||||
|
||||
/* An install root (gl_context_t.sysroot) in canonical form: NULL, "" and
|
||||
* "/" mean the running system and give ""; anything else must be an
|
||||
* absolute path with no "." or ".." components, and loses its trailing
|
||||
* slashes. Room is left for the paths built under it. Returns -1 if
|
||||
* it's refused. */
|
||||
static inline int
|
||||
gl_normalize_sysroot(const char *in, char *out, size_t outsz)
|
||||
{
|
||||
out[0] = '\0';
|
||||
if (!in || in[0] == '\0') { return 0; }
|
||||
if (in[0] != '/') { return -1; }
|
||||
|
||||
size_t len = strlen(in);
|
||||
while (len > 0 && in[len - 1] == '/') { len--; }
|
||||
if (len == 0) { return 0; } /* "/" or "//" */
|
||||
if (len + 256 > outsz || len + 256 > PATH_MAX) { return -1; }
|
||||
|
||||
for (size_t i = 0; i < len; ) {
|
||||
size_t j = i + 1; /* in[i] == '/' */
|
||||
while (j < len && in[j] != '/') { j++; }
|
||||
size_t clen = j - i - 1;
|
||||
const char *comp = in + i + 1;
|
||||
if (clen == 0 || (clen == 1 && comp[0] == '.') ||
|
||||
(clen == 2 && comp[0] == '.' && comp[1] == '.')) {
|
||||
return -1;
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
|
||||
memcpy(out, in, len);
|
||||
out[len] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Is a (normalized, non-empty) install root safe for root to write into?
|
||||
* It and every directory above it must be a real directory (not a
|
||||
* symlink), owned by root, and not group/world-writable: otherwise a
|
||||
* user could swap part of the tree for a symlink to the host's /usr
|
||||
* between two syscalls (IMAGE_DESIGN section 4). Returns 0 if trusted;
|
||||
* otherwise -1, with the offending path in `bad` and `why` set to
|
||||
* "a symlink", "not a directory", or "not root-owned, or is
|
||||
* group/world-writable". */
|
||||
static inline int
|
||||
gl_sysroot_check_one(const char *path, char *bad, size_t badsz, const char **why)
|
||||
{
|
||||
struct stat st;
|
||||
if (lstat(path, &st) != 0) {
|
||||
*why = "missing";
|
||||
} else if (S_ISLNK(st.st_mode)) {
|
||||
*why = "a symlink";
|
||||
} else if (!S_ISDIR(st.st_mode)) {
|
||||
*why = "not a directory";
|
||||
} else if (st.st_uid != 0 || (st.st_mode & 022) != 0) {
|
||||
*why = "not root-owned, or is group/world-writable";
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
snprintf(bad, badsz, "%s", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline int
|
||||
gl_sysroot_trusted(const char *root, char *bad, size_t badsz, const char **why)
|
||||
{
|
||||
if (gl_sysroot_check_one("/", bad, badsz, why) != 0) { return -1; }
|
||||
|
||||
char prefix[PATH_MAX];
|
||||
snprintf(prefix, sizeof(prefix), "%s", root);
|
||||
/* each ancestor, shortest first, then root itself */
|
||||
for (char *p = prefix + 1; *p; p++) {
|
||||
if (*p != '/') { continue; }
|
||||
*p = '\0';
|
||||
int rc = gl_sysroot_check_one(prefix, bad, badsz, why);
|
||||
*p = '/';
|
||||
if (rc != 0) { return -1; }
|
||||
}
|
||||
return gl_sysroot_check_one(prefix, bad, badsz, why);
|
||||
}
|
||||
|
||||
/* mkdir -p. Every directory this creates gets exactly `mode`, whatever
|
||||
* the caller's umask: a system store made under umask 077 would otherwise
|
||||
* be unreachable for every user but root. Directories that already exist
|
||||
* are left alone. */
|
||||
static inline int
|
||||
gl_mkdir_exact(const char *path, mode_t mode)
|
||||
{
|
||||
if (mkdir(path, mode) == 0) {
|
||||
return chmod(path, mode);
|
||||
}
|
||||
return errno == EEXIST ? 0 : -1;
|
||||
}
|
||||
|
||||
static inline int
|
||||
gl_mkdirp(const char *path, mode_t mode)
|
||||
{
|
||||
@@ -88,52 +189,58 @@ gl_mkdirp(const char *path, mode_t mode)
|
||||
for (char *p = tmp + 1; *p; p++) {
|
||||
if (*p == '/') {
|
||||
*p = '\0';
|
||||
if (mkdir(tmp, mode) == -1 && errno != EEXIST) {
|
||||
if (gl_mkdir_exact(tmp, mode) != 0) {
|
||||
return -1;
|
||||
}
|
||||
*p = '/';
|
||||
}
|
||||
}
|
||||
|
||||
if (mkdir(tmp, mode) == -1 && errno != EEXIST) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return gl_mkdir_exact(tmp, mode);
|
||||
}
|
||||
|
||||
/* Empties the directory open at `fd` (and closes it). Every step is
|
||||
* relative to a descriptor opened with O_NOFOLLOW, so nothing is ever
|
||||
* resolved through a symlink: an entry swapped for a symlink mid-walk
|
||||
* (root deleting a tree a user can write to, gstore -x) is unlinked
|
||||
* itself, never followed into its target. */
|
||||
static inline void
|
||||
gl_empty_dir_fd(int fd)
|
||||
{
|
||||
DIR *dir = fdopendir(fd);
|
||||
if (!dir) {
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
struct dirent *ent;
|
||||
while ((ent = readdir(dir))) {
|
||||
/* Only . and ..: dotfiles are content too (a package's own,
|
||||
* the store's .displaced tree). */
|
||||
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
|
||||
continue;
|
||||
}
|
||||
int sub = openat(dirfd(dir), ent->d_name,
|
||||
O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (sub >= 0) {
|
||||
gl_empty_dir_fd(sub);
|
||||
unlinkat(dirfd(dir), ent->d_name, AT_REMOVEDIR);
|
||||
} else {
|
||||
unlinkat(dirfd(dir), ent->d_name, 0);
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
/* rm -r of a directory. `path` itself must be a directory, not a
|
||||
* symlink to one (refused, -1). */
|
||||
static inline int
|
||||
gl_rmdir_recursive(const char *path)
|
||||
{
|
||||
DIR *dir = opendir(path);
|
||||
if (!dir) { return -1; }
|
||||
|
||||
struct dirent *ent;
|
||||
while ((ent = readdir(dir))) {
|
||||
/* Only . and .. — skipping every dotfile left any directory
|
||||
* containing one (a package's own dotfiles, the store's
|
||||
* .displaced tree) impossible to rmdir, so the old/ and stage
|
||||
* wipes silently failed and the next commit hit ENOTEMPTY. */
|
||||
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
char full[PATH_MAX];
|
||||
snprintf(full, sizeof(full), "%s/%s", path, ent->d_name);
|
||||
|
||||
struct stat st;
|
||||
if (lstat(full, &st) != 0) { continue; }
|
||||
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
gl_rmdir_recursive(full);
|
||||
}
|
||||
else {
|
||||
unlink(full);
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
return rmdir(path);
|
||||
int fd = open(path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (fd < 0) { return -1; }
|
||||
gl_empty_dir_fd(fd);
|
||||
return rmdir(path);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user