staging 4.0.0-rc

This commit is contained in:
lw-everestlinux committed 2026-10-29 09:10:02 -04:00
1 parent ae54f74070
commit fb02622b5b
30 files changed
+2346 -2671

No files matched your search

+324 -186
View File
@@ -214,13 +214,14 @@ gl_init_user(uid_t uid, bool isVerbose)
* the next `gstore -n` would refuse as "already exists". mkdir() is
* subject to umask, so chmod explicitly: gl_usr_base_check insists
* on no group/other bits. */
const char *subdirs[] = { "index", "store", "links" };
/* config: the user's build settings (BUILD_DESIGN.txt section 5). */
const char *subdirs[] = { "index", "store", "links", "config" };
char sub[PATH_MAX];
char ind_path[PATH_MAX];
snprintf(ind_path, sizeof(ind_path), "%s/index", base);
if (chmod(base, 0700) != 0) { goto fail; }
for (int i = 0; i < 3; i++) {
for (int i = 0; i < 4; i++) {
snprintf(sub, sizeof(sub), "%s/%s", base, subdirs[i]);
if (isVerbose) { lg_printf(0, "Creating %s", sub); }
if (mkdir(sub, 0700) != 0 || chown(sub, uid, (gid_t)-1) != 0) {
@@ -767,6 +768,71 @@ gl_set_pkg_auto(gl_context_t *ctx, const char *repo, const char *name, bool auto
return gl_rebuild_index(ctx) == 0 ? 0 : -1;
}
/*
* gl_set_pkg_src
* DESCRIPTION: records that the installed repo/name was built from the
* recipe repository src_repo (BUILD_DESIGN.txt section 8): the
* marker GL_SRC_MARKER in its version dir holds "SRC_REPO\n".
* -u then rebuilds it from there. Reinstalling the package
* from a binary drops it (a fresh version dir).
* RETURNS: 0, or -1 if the package isn't installed, src_repo isn't a
* valid name, or the marker can't be written
*/
int
gl_set_pkg_src(gl_context_t *ctx, const char *repo, const char *name, const char *src_repo)
{
if (!ctx || !gl_valid_pkg_ident(repo) || !gl_valid_pkg_ident(name) ||
!gl_valid_pkg_ident(src_repo)) {
return -1;
}
char vdir[PATH_MAX], dirname[NAME_MAX + 1], marker[PATH_MAX];
if (find_version_dir(ctx->store_path, repo, name, vdir, sizeof(vdir),
dirname, sizeof(dirname)) != 0) {
return -1;
}
snprintf(marker, sizeof(marker), "%s/" GL_SRC_MARKER, vdir);
int fd = open(marker, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW,
gl_scope_file_mode(ctx->scope));
if (fd < 0) { return -1; }
size_t len = strlen(src_repo);
bool ok = fchmod(fd, gl_scope_file_mode(ctx->scope)) == 0 &&
write(fd, src_repo, len) == (ssize_t)len && write(fd, "\n", 1) == 1;
close(fd);
return ok ? 0 : -1;
}
/*
* gl_pkg_src
* DESCRIPTION: the recipe repository the installed repo/name was built
* from (gl_set_pkg_src), into out (len bytes).
* RETURNS: 1 with out set; 0 if it was not built from source (no
* marker); -1 if it isn't installed or the marker is unreadable or
* malformed
*/
int
gl_pkg_src(gl_context_t *ctx, const char *repo, const char *name, char *out, size_t len)
{
if (!ctx || !out || len == 0 || !gl_valid_pkg_ident(repo) || !gl_valid_pkg_ident(name)) {
return -1;
}
char vdir[PATH_MAX], dirname[NAME_MAX + 1], marker[PATH_MAX];
if (find_version_dir(ctx->store_path, repo, name, vdir, sizeof(vdir),
dirname, sizeof(dirname)) != 0) {
return -1;
}
snprintf(marker, sizeof(marker), "%s/" GL_SRC_MARKER, vdir);
int fd = open(marker, O_RDONLY | O_NOFOLLOW);
if (fd < 0) { return errno == ENOENT ? 0 : -1; }
char buf[NAME_MAX + 2];
ssize_t n = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (n < 2 || buf[n - 1] != '\n') { return -1; }
buf[n - 1] = '\0';
if (!gl_valid_pkg_ident(buf) || strlen(buf) >= len) { return -1; }
snprintf(out, len, "%s", buf);
return 1;
}
/*
* Archive the installed version of repo/name back into a .gpkg at
* out_path (same layout gl_install_pkg reads: NAME-VER/manifest.gpm.cfg
@@ -1848,6 +1914,259 @@ gl_read_pkg_manifest(const char *gpkg_path, gl_pkg_t *pkg)
return GL_INSTALL_OK;
}
/*
* Extract an open package archive's strip_prefix entries
* ("NAME-VER/files/") into the existing directory dest, with every rule
* package extraction has (see the comments inside): no "..", no
* absolute paths, no reserved .glacier-* names, never writing through a
* symlink, symlinks last, hardlinks refused, devices/fifos skipped, modes
* GL_ENTRY_MODE. manifest_entry, if not NULL, is copied to
* dest/GL_MANIFEST_COPY. What it made in dest is left for the caller to
* remove on failure.
*/
static gl_install_status_t
extract_tree(struct archive *a, const char *dest, const char *strip_prefix,
const char *manifest_entry, mode_t dir_mode)
{
struct archive_entry *entry;
/* Symlink entries are collected here and only created once every
* directory and regular file is in place. While regular content is
* being written the package dir therefore contains no symlinks at
* all, so no entry can be written *through* one — the classic
* symlink tar-slip, where `x -> /etc` followed by `x/passwd` writes
* the real /etc/passwd, as root under syspkg. crosses_symlink() and
* O_NOFOLLOW below are the second line of defense, and also cover
* a deferred symlink whose parent is another deferred symlink. */
struct deferred_link { char *dest; char *target; } *links = NULL;
size_t nlinks = 0;
gl_install_status_t ret = GL_INSTALL_ERR_EXTRACT;
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
const char *entry_path = archive_entry_pathname(entry);
/* Keep the manifest beside the files (GL_MANIFEST_COPY). */
if (manifest_entry && strcmp(entry_path, manifest_entry) == 0 &&
archive_entry_filetype(entry) == AE_IFREG) {
char mpath[PATH_MAX];
snprintf(mpath, sizeof(mpath), "%s/" GL_MANIFEST_COPY, dest);
int mfd = open(mpath, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644);
if (mfd < 0) { goto extract_fail; }
char buffer[8192];
ssize_t n;
while ((n = archive_read_data(a, buffer, sizeof(buffer))) > 0) {
if (write(mfd, buffer, (size_t)n) != n) {
close(mfd);
goto extract_fail;
}
}
close(mfd);
continue;
}
if (strncmp(entry_path, strip_prefix, strlen(strip_prefix)) != 0) {
archive_read_data_skip(a);
continue;
}
const char *rel_path = entry_path + strlen(strip_prefix);
if (rel_path[0] == '\0') {
archive_read_data_skip(a);
continue;
}
/* Refuse the whole package rather than skip the entry: an
* archive carrying a traversal path is hostile or broken, and
* installing the rest of it would be a partial install of
* something we've already stopped trusting. */
if (!is_safe_rel_path(rel_path)) {
lg_printf(2, "Refusing unsafe archive entry path '%s'.",
entry_path);
goto extract_fail;
}
/* A hardlink entry has no content of its own: skipping it
* silently dropped files packages ship (binutils' ld is a
* hardlink to ld.bfd). Packers store hardlinks as plain files
* (gbuild, build-driver.sh); a package that has one anyway is
* refused, never installed with files missing. */
if (archive_entry_hardlink(entry)) {
lg_printf(2, "Refusing archive entry '%s': it is a hardlink.",
entry_path);
goto extract_fail;
}
/* Top-level .glacier-* names are glacier's own files in the
* version dir (GL_MARKER_PREFIX). A package shipping one could
* grant itself -O, rewrite its saved manifest, or, as a
* symlink, redirect glacier's marker write (as root, under
* syspkg) to any file on the system. */
if (strncmp(rel_path, GL_MARKER_PREFIX, strlen(GL_MARKER_PREFIX)) == 0) {
lg_printf(2, "Refusing archive entry '%s': %s* names are reserved.",
entry_path, GL_MARKER_PREFIX);
goto extract_fail;
}
char dest_path[PATH_MAX];
snprintf(dest_path, sizeof(dest_path), "%s/%s",
dest, rel_path);
if (crosses_symlink(dest, dest_path)) {
lg_printf(2, "Refusing archive entry '%s': a parent directory is a symlink.",
entry_path);
goto extract_fail;
}
switch (archive_entry_filetype(entry)) {
case AE_IFDIR:
if (gl_mkdir_exact(dest_path, GL_ENTRY_MODE(entry)) != 0) {
goto extract_fail;
}
break;
case AE_IFREG: {
char parent[PATH_MAX];
snprintf(parent, sizeof(parent), "%s", dest_path);
char *slash = strrchr(parent, '/');
if (slash) {
*slash = '\0';
gl_mkdirp(parent, dir_mode);
}
int fd = open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW,
GL_ENTRY_MODE(entry));
if (fd < 0) { goto extract_fail; }
if (fchmod(fd, GL_ENTRY_MODE(entry)) != 0) {
close(fd);
goto extract_fail;
}
char buffer[8192];
ssize_t n;
while ((n = archive_read_data(a, buffer, sizeof(buffer))) > 0) {
ssize_t written = write(fd, buffer, (size_t)n);
if (written < 0 || written != n) {
close(fd);
goto extract_fail;
}
}
close(fd);
break;
}
case AE_IFLNK: {
/* The target is stored verbatim and means what it says
* at RUNTIME: musl's lib/ld-musl-x86_64.so.1 ->
* /usr/lib/libc.so resolves against the running system,
* not the store. Nothing glacier does afterwards writes
* through a store symlink (removal and seeding lstat()). */
const char *target = archive_entry_symlink(entry);
if (!target || target[0] == '\0') {
lg_printf(2, "Refusing archive entry '%s': symlink with no target.",
entry_path);
goto extract_fail;
}
struct deferred_link *grown = realloc(links,
(nlinks + 1) * sizeof(*links));
if (!grown) { goto extract_fail; }
links = grown;
links[nlinks].dest = strdup(dest_path);
links[nlinks].target = strdup(target);
nlinks++;
if (!links[nlinks - 1].dest || !links[nlinks - 1].target) {
goto extract_fail;
}
break;
}
default:
/* Devices, fifos: nothing a package needs, and nothing
* worth trusting an archive to create. Skipped. (Hardlinks
* are refused above.) */
archive_read_data_skip(a);
break;
}
}
for (size_t i = 0; i < nlinks; i++) {
if (crosses_symlink(dest, links[i].dest)) {
lg_printf(2, "Refusing symlink '%s': a parent directory is a symlink.",
links[i].dest);
goto extract_fail;
}
char parent[PATH_MAX];
snprintf(parent, sizeof(parent), "%s", links[i].dest);
char *slash = strrchr(parent, '/');
if (slash) {
*slash = '\0';
gl_mkdirp(parent, dir_mode);
}
/* symlink() fails EEXIST rather than replacing: an archive that
* ships both a file/dir and a symlink at one path is refused. */
if (symlink(links[i].target, links[i].dest) != 0) {
lg_printf(2, "Cannot create symlink %s: %s", links[i].dest, strerror(errno));
goto extract_fail;
}
}
ret = GL_INSTALL_OK;
extract_fail:
for (size_t i = 0; i < nlinks; i++) {
free(links[i].dest);
free(links[i].target);
}
free(links);
return ret;
}
/*
* gl_extract_files
* DESCRIPTION: extracts a package's files/ tree into dest, a directory
* this call creates (0755; it must not exist yet), with every
* rule an install has (extract_tree). For build layers
* (BUILD_DESIGN.txt section 6): nothing is installed, linked or
* indexed. On failure, dest is removed.
* RETURNS: GL_INSTALL_OK, GL_INSTALL_ERR_OPEN, GL_INSTALL_ERR_MANIFEST
* or GL_INSTALL_ERR_EXTRACT (including dest existing)
*/
gl_install_status_t
gl_extract_files(const char *gpkg_path, const char *dest)
{
gl_pkg_t pkg = {0};
int mrc = gl_read_pkg_manifest(gpkg_path, &pkg);
if (mrc != GL_INSTALL_OK) {
return (gl_install_status_t)mrc;
}
char strip_prefix[PATH_MAX];
snprintf(strip_prefix, sizeof(strip_prefix), "%s-%d.%d.%d/files/", pkg.pkg_name,
pkg.pkg_ver.maj, pkg.pkg_ver.min, pkg.pkg_ver.pat);
gl_free_pkg(&pkg);
if (mkdir(dest, 0755) != 0) {
return GL_INSTALL_ERR_EXTRACT;
}
gl_install_status_t ret = GL_INSTALL_ERR_EXTRACT;
struct archive *a = archive_read_new();
archive_read_support_filter_gzip(a);
archive_read_support_format_tar(a);
if (chmod(dest, 0755) == 0 &&
archive_read_open_filename(a, gpkg_path, 10240) == ARCHIVE_OK) {
ret = extract_tree(a, dest, strip_prefix, NULL, 0755);
}
archive_read_free(a);
if (ret != GL_INSTALL_OK) {
gl_rmdir_recursive(dest);
}
return ret;
}
gl_install_status_t
gl_install_pkg(gl_context_t *ctx, const char *gpkg_path)
{
@@ -1931,7 +2250,6 @@ gl_install_pkg(gl_context_t *ctx, const char *gpkg_path)
}
/* now the archive must be reopened in order to extract package files */
struct archive_entry *entry;
struct archive *a2 = archive_read_new();
archive_read_support_filter_gzip(a2);
archive_read_support_format_tar(a2);
@@ -1947,193 +2265,14 @@ gl_install_pkg(gl_context_t *ctx, const char *gpkg_path)
"%s-%d.%d.%d/files/", pkg.pkg_name,
pkg.pkg_ver.maj, pkg.pkg_ver.min, pkg.pkg_ver.pat);
/* Symlink entries are collected here and only created once every
* directory and regular file is in place. While regular content is
* being written the package dir therefore contains no symlinks at
* all, so no entry can be written *through* one — the classic
* symlink tar-slip, where `x -> /etc` followed by `x/passwd` writes
* the real /etc/passwd, as root under syspkg. crosses_symlink() and
* O_NOFOLLOW below are the second line of defense, and also cover
* a deferred symlink whose parent is another deferred symlink. */
struct deferred_link { char *dest; char *target; } *links = NULL;
size_t nlinks = 0;
gl_install_status_t ret = GL_INSTALL_ERR_EXTRACT;
char manifest_entry[PATH_MAX];
snprintf(manifest_entry, sizeof(manifest_entry), "%s-%d.%d.%d/manifest.gpm.cfg",
pkg.pkg_name, pkg.pkg_ver.maj, pkg.pkg_ver.min, pkg.pkg_ver.pat);
while (archive_read_next_header(a2, &entry) == ARCHIVE_OK) {
const char *entry_path = archive_entry_pathname(entry);
/* Keep the manifest beside the files (GL_MANIFEST_COPY). */
if (strcmp(entry_path, manifest_entry) == 0 &&
archive_entry_filetype(entry) == AE_IFREG) {
char mpath[PATH_MAX];
snprintf(mpath, sizeof(mpath), "%s/" GL_MANIFEST_COPY, pkg_store_final);
int mfd = open(mpath, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644);
if (mfd < 0) { goto extract_fail; }
char buffer[8192];
ssize_t n;
while ((n = archive_read_data(a2, buffer, sizeof(buffer))) > 0) {
if (write(mfd, buffer, (size_t)n) != n) {
close(mfd);
goto extract_fail;
}
}
close(mfd);
continue;
}
if (strncmp(entry_path, strip_prefix, strlen(strip_prefix)) != 0) {
archive_read_data_skip(a2);
continue;
}
const char *rel_path = entry_path + strlen(strip_prefix);
if (rel_path[0] == '\0') {
archive_read_data_skip(a2);
continue;
}
/* Refuse the whole package rather than skip the entry: an
* archive carrying a traversal path is hostile or broken, and
* installing the rest of it would be a partial install of
* something we've already stopped trusting. */
if (!is_safe_rel_path(rel_path)) {
lg_printf(2, "Refusing unsafe archive entry path '%s'.",
entry_path);
goto extract_fail;
}
/* Top-level .glacier-* names are glacier's own files in the
* version dir (GL_MARKER_PREFIX). A package shipping one could
* grant itself -O, rewrite its saved manifest, or, as a
* symlink, redirect glacier's marker write (as root, under
* syspkg) to any file on the system. */
if (strncmp(rel_path, GL_MARKER_PREFIX, strlen(GL_MARKER_PREFIX)) == 0) {
lg_printf(2, "Refusing archive entry '%s': %s* names are reserved.",
entry_path, GL_MARKER_PREFIX);
goto extract_fail;
}
char dest_path[PATH_MAX];
snprintf(dest_path, sizeof(dest_path), "%s/%s",
pkg_store_final, rel_path);
if (crosses_symlink(pkg_store_final, dest_path)) {
lg_printf(2, "Refusing archive entry '%s': a parent directory is a symlink.",
entry_path);
goto extract_fail;
}
switch (archive_entry_filetype(entry)) {
case AE_IFDIR:
if (gl_mkdir_exact(dest_path, GL_ENTRY_MODE(entry)) != 0) {
goto extract_fail;
}
break;
case AE_IFREG: {
char parent[PATH_MAX];
snprintf(parent, sizeof(parent), "%s", dest_path);
char *slash = strrchr(parent, '/');
if (slash) {
*slash = '\0';
gl_mkdirp(parent, dir_mode);
}
int fd = open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW,
GL_ENTRY_MODE(entry));
if (fd < 0) { goto extract_fail; }
if (fchmod(fd, GL_ENTRY_MODE(entry)) != 0) {
close(fd);
goto extract_fail;
}
char buffer[8192];
ssize_t n;
while ((n = archive_read_data(a2, buffer, sizeof(buffer))) > 0) {
ssize_t written = write(fd, buffer, (size_t)n);
if (written < 0 || written != n) {
close(fd);
goto extract_fail;
}
}
close(fd);
break;
}
case AE_IFLNK: {
/* The target is stored verbatim and means what it says
* at RUNTIME: musl's lib/ld-musl-x86_64.so.1 ->
* /usr/lib/libc.so resolves against the running system,
* not the store. Nothing glacier does afterwards writes
* through a store symlink (removal and seeding lstat()). */
const char *target = archive_entry_symlink(entry);
if (!target || target[0] == '\0') {
lg_printf(2, "Refusing archive entry '%s': symlink with no target.",
entry_path);
goto extract_fail;
}
struct deferred_link *grown = realloc(links,
(nlinks + 1) * sizeof(*links));
if (!grown) { goto extract_fail; }
links = grown;
links[nlinks].dest = strdup(dest_path);
links[nlinks].target = strdup(target);
nlinks++;
if (!links[nlinks - 1].dest || !links[nlinks - 1].target) {
goto extract_fail;
}
break;
}
default:
/* Hardlinks, devices, fifos: nothing a package needs, and
* nothing worth trusting an archive to create. Skipped,
* same as before symlinks were supported. */
archive_read_data_skip(a2);
break;
}
}
for (size_t i = 0; i < nlinks; i++) {
if (crosses_symlink(pkg_store_final, links[i].dest)) {
lg_printf(2, "Refusing symlink '%s': a parent directory is a symlink.",
links[i].dest);
goto extract_fail;
}
char parent[PATH_MAX];
snprintf(parent, sizeof(parent), "%s", links[i].dest);
char *slash = strrchr(parent, '/');
if (slash) {
*slash = '\0';
gl_mkdirp(parent, dir_mode);
}
/* symlink() fails EEXIST rather than replacing: an archive that
* ships both a file/dir and a symlink at one path is refused. */
if (symlink(links[i].target, links[i].dest) != 0) {
lg_printf(2, "Cannot create symlink %s: %s", links[i].dest, strerror(errno));
goto extract_fail;
}
}
ret = GL_INSTALL_OK;
extract_fail:
for (size_t i = 0; i < nlinks; i++) {
free(links[i].dest);
free(links[i].target);
}
free(links);
gl_install_status_t ret = extract_tree(a2, pkg_store_final, strip_prefix,
manifest_entry, dir_mode);
archive_read_free(a2);
if (ret != GL_INSTALL_OK) {
archive_read_free(a2);
/* Remove the half-extracted version dir (created by this call,
* just above). Left behind, it made the next attempt report
* GL_INSTALL_ERR_ALREADY_INSTALLED — harmless in a stage that's
@@ -2142,7 +2281,6 @@ extract_fail:
gl_free_pkg(&pkg);
return ret;
}
archive_read_free(a2);
/* now the index will be updated */
/* Resolve "current" symlink to confirm an index exists to rebuild