From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from atuin.qyliss.net (localhost [IPv6:::1]) by atuin.qyliss.net (Postfix) with ESMTP id 0C0991B609; Wed, 19 Nov 2025 08:21:22 +0000 (UTC) Received: by atuin.qyliss.net (Postfix, from userid 993) id 9F4291B549; Wed, 19 Nov 2025 08:21:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on atuin.qyliss.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DMARC_PASS,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=4.0.1 Received: from mail-yw1-x112b.google.com (mail-yw1-x112b.google.com [IPv6:2607:f8b0:4864:20::112b]) by atuin.qyliss.net (Postfix) with ESMTPS id 8E63E1B522 for ; Wed, 19 Nov 2025 08:21:13 +0000 (UTC) Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-787da30c50fso59338837b3.3 for ; Wed, 19 Nov 2025 00:21:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763540472; x=1764145272; darn=spectrum-os.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=7h+WI5y4+AB8tDilj5NHAQR/OHPYPzNk3S/0Q8CBvG4=; b=i+3dP6Nm0z5RManVXzQLo5DFJpcbWOTai8pp/6cq51wbfQUbSn9SxXEH/EN878skez BXjaceJ34A///8mxkkIV5VO5COdDF9xK54kfmM2IsFnxQxsnclIznKXE1gdejuLjyTQ9 u1QQIlCfjOGDMi4O6fpyKgzmlENj7gHD7CtnrJuaplEt45K8Ll6uOH+ckr9pIoTnIx2q KhXUsnaBbC5J2oe9OpzeM1I8kscMjlnHYzLQxzIYugLuSDN6OSrc4k9YeaO0DWH48AzR Qm4bKA1lRrNK1z24Q7Z7QFv4M/9pDjDUZJ05orjaMaJ9WsDd1jZkpx9Lz+LuQHMK1iH6 HRtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763540472; x=1764145272; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=7h+WI5y4+AB8tDilj5NHAQR/OHPYPzNk3S/0Q8CBvG4=; b=xKXpvNg+JUvTTD4F3wDg36GiZGee0RDStXlVaG3DYMogIjf4YO2+ZbhESoekXVv7He 2tNNIU4fnfiRnsrd5pRFc976MZVgCHDrs/mAqFonxN7n4DccVjDAqlEwSaXCmlGs7frU gGQ+PlgqMA3+Ci0dr+DcB5tWaCKuaTiiWFTzz/7INBhPOQsUrRjSHws5haLXuqtRl+u0 gx0pPCa7VfiKAAfeBXpHeZqX+Xs5zaDBckc6r63FR+3tzvaLdS/Hy32xBb/GyQ1wUlHt KEggINS7P3IbdOmosGgVl86Ap3NHMvIFP/TV27SSXV7HSJDxtxezgt17xolp+4u7rhCI ppFA== X-Gm-Message-State: AOJu0YzcQpd0GwSp71VLTFMUIExy5uXL7q8VXXTyVLse5l5F30Ce0jgV MAJDOTJAg6OA+NWGLWm0YExZrY9Vwhpq5jgROJwcEK4/6hRI4p2D/gEyBmj4/w== X-Gm-Gg: ASbGncsbwpsOwPQHVhJ2DZYIxmXC9ziZXJmN9TMoH4yPFJFTZ+Soff5xRMcuPfsLQz4 gIN2d1d3TPko+BNuOQ47AH2NwTiaQAPi8Wfgq62rdY2b/+ykDMdaZo3BIUCaT0lmEXfQuXqa51H 1u8MgBjUa1nfEDD6QshTUYlOwdgn1LFpYPc2p/9rQI91Gy9lUfP6C3AIltsagARj6s54FxwvkWO 1MR+239Q07JKolDp2v9l1vxQ7zASjLM8Q4y/UNbVi7TVPXK4bDPfRAlRxWUfLplICERfON0lXGR zxrIBtdReNQ7eHH1+nHzIZZ496z6j/3kKfn0Oca11g3eSMapMSA8QWjAydssrBf3ekLHtYJD2Mp YW1sz1UEjiRXfdrkeJ/WciV8tPyfqLV0T8v7tZC6X2HG2YQpruoW4S7K56HzN2QbfAUVWXm9H3Y ryzd6JuRjyjh2Kvh/PbxNNtlZkvmL44gMtMFRGR5mijG5+xF4+1zujPoF9cAi+n8z+eHioRTN+k HcrN+/LB1M+0fu2OeRu/pic0mVKhYkZnSstaFxS2SvPAg== X-Google-Smtp-Source: AGHT+IFmvKx3tqZfR3MNameMmKAA7wtRZtFoXx48KvmLMSmN9KaOhND42UmNnz2OYeawZdRkuQGGOw== X-Received: by 2002:a05:690c:30f:b0:786:8331:6a02 with SMTP id 00721157ae682-78929f40ac0mr332073327b3.69.1763540471609; Wed, 19 Nov 2025 00:21:11 -0800 (PST) Received: from localhost.localdomain (h96-60-249-169.cncrtn.broadband.dynamic.tds.net. [96.60.249.169]) by smtp.gmail.com with UTF8SMTPSA id 956f58d0204a3-6410ead559dsm6663366d50.24.2025.11.19.00.21.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Nov 2025 00:21:11 -0800 (PST) From: Demi Marie Obenour Date: Wed, 19 Nov 2025 03:18:26 -0500 Subject: [PATCH v3 03/14] tools: Add directory checker for updates MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20251119-updates-v3-3-b88a99915509@gmail.com> References: <20251119-updates-v3-0-b88a99915509@gmail.com> In-Reply-To: <20251119-updates-v3-0-b88a99915509@gmail.com> To: Spectrum OS Development X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1763540303; l=7423; i=demiobenour@gmail.com; s=20250729; h=from:subject:message-id; bh=bvXhlXimSGhw/9x5ma21/1Ep7wi8XbniXpn33Q8vP10=; b=wIgIvLE35FCfadphikCrAvOoPYtoYVVU/qq/BK0u3JaQ1Pfccrb8B0XWyV7dN//MUn4ydWbu4 BCJvMDqDgfJBIBKoLSMoJtbXmJsgdaFCATGc6IlYlu1ynzVfHHl4rWJ X-Developer-Key: i=demiobenour@gmail.com; a=ed25519; pk=X57Q4/YQDj9t4SBeKaDwvXYKB6quZJVx/DE2Ly2out0= Message-ID-Hash: KQ7RODLNL3MFYPKBTVWXXDKEA3GBLAMH X-Message-ID-Hash: KQ7RODLNL3MFYPKBTVWXXDKEA3GBLAMH X-MailFrom: demiobenour@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-devel.spectrum-os.org-0; header-match-devel.spectrum-os.org-1; header-match-devel.spectrum-os.org-2; header-match-devel.spectrum-os.org-3; header-match-devel.spectrum-os.org-4; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Demi Marie Obenour , Alyssa Ross X-Mailman-Version: 3.3.9 Precedence: list List-Id: Patches and low-level development discussion Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Spectrum OS's host has no network access. Updates must be downloaded by VMs. The downloads are placed into a bind-mounted directory. The VM can write whatever it wants into that directory. This includes symlinks that subsequent code might open, which would create a path traversal vulnerability. It also includes paths with names containing containing terminal escape sequences, newlines, or other nastiness. Furthermore, the directory should not have any subdirectories either. Add a simple C program that checks for such ugliness and indicates (via its exit code) if the VM misbehaved. systemd-sysupdate can leave behind temporary files with names starting with '.', so delete them instead of failing. Linux can lose cache coherency if there is an I/O error, so call syncfs() on the directory before checking anything. For the same reason, fsync() the directory if any hidden files were deleted. The directory checker also serves another critical function: it checks if the VM actually downloaded anything. Otherwise, network problems could cause updates to silently do nothing. Specifically, it checks that the VM provided a file starting with the prefix "SHA256SUMS.". These will be the last ones the in-VM updater downloads. An additional mode is provided to clean out all such files. This will be used to ensure that before the in-VM updater runs, no such files are present. Hence, if the VM didn't actually download anything, the user will get a clear error instead of a false success message or a confusing error. Signed-off-by: Demi Marie Obenour --- Changes since v2: - Purge leftover temporary files rather than returning an error. - Split into two modes: one that deletes signature files, and one that checks that at least one signature file exists. This allows checking that the VM actually sent something. --- tools/default.nix | 1 + tools/meson.build | 4 ++ tools/updates-dir-check.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) diff --git a/tools/default.nix b/tools/default.nix index 27d4e152c8ef2ac16c791e57d70e68959789e446..1aaa5b35471892df1a35313f46e5489f8e10d755 100644 --- a/tools/default.nix +++ b/tools/default.nix @@ -77,6 +77,7 @@ stdenv.mkDerivation (finalAttrs: { ./sd-notify-adapter.c ./start-vmm ./subprojects + ./updates-dir-check.c ] ++ lib.optionals driverSupport [ ./xdp-forwarder ])); diff --git a/tools/meson.build b/tools/meson.build index d465e99c2ef597fdf7e818748d08db3d96f4ec6b..a7c21684dd64ad9e87c85bcdf31792e81b55faa4 100644 --- a/tools/meson.build +++ b/tools/meson.build @@ -29,6 +29,10 @@ if get_option('host') c_args : '-D_GNU_SOURCE', install: true) + executable('updates-dir-check', 'updates-dir-check.c', + c_args : '-D_GNU_SOURCE', + install: true) + subdir('lsvm') subdir('start-vmm') endif diff --git a/tools/updates-dir-check.c b/tools/updates-dir-check.c new file mode 100644 index 0000000000000000000000000000000000000000..07eb059f2718e1ad8ab087fe6509c1437ea3e96c --- /dev/null +++ b/tools/updates-dir-check.c @@ -0,0 +1,133 @@ +// SPDX-License-Identifier: EUPL-1.2+ +// SPDX-FileCopyrightText: 2025 Demi Marie Obenour +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +[[noreturn]] static void bad_char(char c, char *msg_component) +{ + if (c >= 0x20 && c <= 0x7E) + errx(EXIT_FAILURE, "Forbidden %s character in filename: '%c'", + msg_component, (int)c); + errx(EXIT_FAILURE, + "Forbidden %s character in filename: byte %d", + msg_component, (int)(unsigned char)c); +} + +[[noreturn]] static void usage(void) +{ + errx(EXIT_FAILURE, "Usage: updates-dir-check [cleanup|check] DIRECTORIES..."); +} + +static void checkdir(int fd, bool check_sig) +{ + bool found_sig = false; + DIR *d = fdopendir(fd); + if (d == NULL) + err(EXIT_FAILURE, "fdopendir"); + // If there is an I/O error while there are dirty pages outstanding, + // the dirty pages are silently discarded. This means that the contents + // of the filesystem can change behind userspace's back. Flush all + // dirty pages in the filesystem with the directory to prevent this. + if (syncfs(fd) != 0) + err(EXIT_FAILURE, "syncfs"); + bool changed = false; + for (;;) { + errno = 0; + struct dirent *entry = readdir(d); + if (entry == NULL) { + if (errno) + err(EXIT_FAILURE, "readdir"); + break; + } + const char *ptr = entry->d_name; + if (ptr[0] == '.') { + if (ptr[1] == '\0') + continue; + if (ptr[1] == '.' && ptr[2] == '\0') + continue; + // systemd-sysupdate uses these for temporary files. + // It normally cleans them up itself, but if there is an error + // it does not always clean them up. I'm not sure if it is + // guaranteed to clean up temporary files from a past run, so + // delete them instead of returning an error. + if (unlinkat(fd, ptr, 0)) + err(EXIT_FAILURE, "Failed to unlink temporary file"); + changed = true; + continue; + } + char c = ptr[0]; + if (!((c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z'))) + bad_char(c, "initial"); + while ((c = *++ptr)) { + if (!((c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9') || + (c == '_') || + (c == '-') || + (c == '.'))) + bad_char(c, "subsequent"); + } + // Empty filenames are rejected as having a bad initial character, + // and POSIX forbids them from being returned anyway. Therefore, + // this cannot be out of bounds. + if (ptr[-1] == '.') + errx(EXIT_FAILURE, "Filename %s ends with a '.'", entry->d_name); + if (entry->d_type == DT_UNKNOWN) + errx(EXIT_FAILURE, "Filesystem didn't report type of file %s", entry->d_name); + if (entry->d_type != DT_REG) + errx(EXIT_FAILURE, "Entry contains non-regular file %s", entry->d_name); + if (strncmp(entry->d_name, "SHA256SUMS.", sizeof("SHA256SUMS.") - 1) == 0) { + // Found a signature file! + if (check_sig) + found_sig = true; + else { + if (unlinkat(fd, entry->d_name, 0)) + err(EXIT_FAILURE, "Unlinking old signature file"); + changed = true; + } + } + } + // fsync() the directory if it was changed, to avoid the above + // cache-incoherency problem. + if (changed && fsync(fd)) + errx(EXIT_FAILURE, "fsync"); + if (check_sig && !found_sig) { + warnx("sys.appvm-systemd-sysupdate didn't send a signature file."); + warnx("There was probably a problem downloading the update."); + errx(EXIT_FAILURE, "Check its logs for more information."); + } + closedir(d); +} + +int main(int argc, char **argv) +{ + if (argc != 3) + usage(); + + bool check_sig; + if (strcmp(argv[1], "cleanup") == 0) + check_sig = false; + else if (strcmp(argv[1], "check") == 0) + check_sig = true; + else + usage(); + + for (int i = 2; i < argc; ++i) { + int fd = open(argv[i], O_DIRECTORY|O_RDONLY|O_CLOEXEC); + if (fd < 0) + err(EXIT_FAILURE, "open(%s)", argv[i]); + checkdir(fd, check_sig); + } + return 0; +} -- 2.52.0