#ifndef COMMON_H_ #define COMMON_H_ #define _POSIX_C_SOURCE 200809L #define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include #include #include /* * On-disk layout. Each scope keeps everything it ever creates, renames * or locks under ONE base directory it owns: * * /glacier/usr//{index,store,links,stage,old,lock} owned by uid, 0700 * /glacier/sys/{index,store,links,stage,old,lock} root * * /glacier/usr itself is root-owned and not group/world-writable, so a * user can never create (or squat) another uid's base; root creates * each one (gl_init_user, via `gstore -n` directly or through doas). * Every transaction step — stage, old/, lock, the commit renames — then * happens inside a directory the acting user owns, which is what lets * 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" /* /.glacier-src: built from this recipe repository (gl_set_pkg_src). */ #define GL_SRC_MARKER ".glacier-src" /* 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 gl_usr_base(uid_t uid, char *out, size_t outsz) { snprintf(out, outsz, GL_USR_ROOT "/%u", (unsigned)uid); } typedef enum { GL_USR_BASE_OK = 0, GL_USR_BASE_MISSING, /* no /glacier/usr/ yet: run gstore -n */ GL_USR_BASE_UNSAFE_ROOT, /* /glacier/usr not root-owned, or group/world-writable */ GL_USR_BASE_UNSAFE /* not a real dir, not owned by uid, or not 0700-ish */ } gl_usr_base_status_t; /* Refuse to operate on a user base we didn't hand out. lstat(), never * stat(): a symlink in place of /glacier/usr/ is exactly what a * squatter would plant. Backstop for the layout invariant above, so a * hand-loosened /glacier/usr can't silently turn into one user writing * another's store. */ static inline gl_usr_base_status_t gl_usr_base_check(uid_t uid) { struct stat st; if (lstat(GL_USR_ROOT, &st) != 0) { return GL_USR_BASE_MISSING; } if (!S_ISDIR(st.st_mode) || st.st_uid != 0 || (st.st_mode & (S_IWGRP | S_IWOTH)) != 0) { return GL_USR_BASE_UNSAFE_ROOT; } char base[PATH_MAX]; gl_usr_base(uid, base, sizeof(base)); if (lstat(base, &st) != 0) { return GL_USR_BASE_MISSING; } if (!S_ISDIR(st.st_mode) || st.st_uid != uid || (st.st_mode & (S_IRWXG | S_IRWXO)) != 0) { return GL_USR_BASE_UNSAFE; } 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) { char tmp[PATH_MAX]; strncpy(tmp, path, sizeof(tmp)); tmp[sizeof(tmp) - 1] = '\0'; for (char *p = tmp + 1; *p; p++) { if (*p == '/') { *p = '\0'; if (gl_mkdir_exact(tmp, mode) != 0) { return -1; } *p = '/'; } } 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) { 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); } #endif