mxc icon indicating copy to clipboard operation
mxc copied to clipboard

Process container: rename relative to a directory handle fails with STATUS_INVALID_HANDLE (`FILE_RENAME_INFORMATION.RootDirectory`)

Open altohq opened this issue 2 weeks ago • 1 comments

Relevant area(s): Windows

Brief description of your issue

Inside a processcontainer (tier base-container, PSEC 1.0), NtSetInformationFile with FileRenameInformation or FileRenameInformationEx returns STATUS_INVALID_HANDLE (0xC0000008) when FILE_RENAME_INFORMATION.RootDirectory is a handle to the target directory and FileName is a name relative to it. The file and the directory are both under a readwritePaths entry, and the handle is valid: it was opened in the same process a line earlier.

The same rename succeeds inside the container when RootDirectory is null and FileName is a full \??\C:\… path, and both forms succeed outside the container. So this is separate from #1676 (rename by path denied with "Access is denied"); on this build a rename by path works.

Programs that call the NT API directly rename this way. Zig's standard library does whenever the new path isn't absolute, and its compiler moves every compilation's output into its cache with it, so inside the container zig build-exe of an empty main fails ("failed to rename compilation results … into local cache"), and nothing that builds with Zig can be built there.

  • Windows 11 Pro 26H2, build 26300.9974 (x64)
  • @microsoft/mxc-sdk 1.0.1, bin\x64\wxc-exec.exe, request contract 1.0.0

Steps to reproduce

probe.c (below) makes the rename both ways and prints each status. In an x64 Native Tools Command Prompt, from the folder holding probe.c and config.json:

mkdir C:\repro\work C:\repro\ro
copy probe.c C:\repro
copy config.json C:\repro
cd /d C:\repro
npm install @microsoft/mxc-sdk
cl probe.c ntdll.lib
probe.exe C:\repro\work C:\repro\ro
node_modules\@microsoft\mxc-sdk\bin\x64\wxc-exec.exe config.json

config.json:

{
  "version": "1.0.0",
  "containment": "processcontainer",
  "filesystem": {
    "readonlyPaths": ["C:\\"],
    "readwritePaths": ["C:\\repro\\work"]
  },
  "network": { "egress": { "default": "deny" } },
  "process": { "commandLine": "C:\\repro\\probe.exe C:\\repro\\work C:\\repro\\ro" }
}

probe.c:

// Two file calls that succeed outside Windows' process sandbox and fail
// inside it. Each is made the way that works and the way that doesn't.
//
//   probe.exe WRITABLE_DIR READONLY_DIR
//
// Build: cl probe.c ntdll.lib
//    or: zig cc probe.c -o probe.exe -municode -lntdll
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>

typedef struct { union { LONG Status; PVOID Pointer; }; ULONG_PTR Information; } IOSB;
typedef struct { USHORT Length, MaximumLength; PWSTR Buffer; } USTR;
typedef struct { ULONG Length; HANDLE RootDirectory; USTR *ObjectName; ULONG Attributes; PVOID SecurityDescriptor, SecurityQualityOfService; } OATTR;
typedef struct { ULONG Flags; HANDLE RootDirectory; ULONG FileNameLength; WCHAR FileName[1]; } RENAME_INFO;

LONG NTAPI NtSetInformationFile(HANDLE, IOSB *, PVOID, ULONG, int);
LONG NTAPI NtCreateFile(HANDLE *, ULONG, OATTR *, IOSB *, LARGE_INTEGER *, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG);

static void touch(const wchar_t *path) {
    HANDLE h = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (h != INVALID_HANDLE_VALUE) CloseHandle(h);
}

// NtSetInformationFile(FileRenameInformationEx): `from` to `name` in `dir`,
// by a full path with no RootDirectory, or by `name` alone with RootDirectory
// a handle to `dir`.
static LONG rename_file(const wchar_t *from, const wchar_t *dir, const wchar_t *name, int relative) {
    wchar_t target[1024];
    if (relative) swprintf(target, 1024, L"%ls", name); else swprintf(target, 1024, L"\\??\\%ls\\%ls", dir, name);
    ULONG bytes = (ULONG)(wcslen(target) * sizeof(WCHAR));
    ULONG size = (ULONG)(FIELD_OFFSET(RENAME_INFO, FileName) + bytes);
    RENAME_INFO *info = calloc(1, size + sizeof(WCHAR));
    HANDLE file = CreateFileW(from, DELETE | SYNCHRONIZE, 7, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
    HANDLE root = relative ? CreateFileW(dir, FILE_LIST_DIRECTORY | SYNCHRONIZE, 7, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL) : NULL;
    info->Flags = 0x43; // FILE_RENAME_REPLACE_IF_EXISTS | FILE_RENAME_POSIX_SEMANTICS | FILE_RENAME_IGNORE_READONLY_ATTRIBUTE
    info->RootDirectory = root;
    info->FileNameLength = bytes;
    memcpy(info->FileName, target, bytes);
    IOSB io;
    LONG status = NtSetInformationFile(file, &io, info, size, 65 /* FileRenameInformationEx */);
    CloseHandle(file);
    if (root) CloseHandle(root);
    free(info);
    return status;
}

// NtCreateFile on a directory: FILE_OPEN, read access only, with or without
// FILE_DIRECTORY_FILE among the create options.
static LONG open_dir(const wchar_t *path, int directory_file) {
    wchar_t nt[1024];
    swprintf(nt, 1024, L"\\??\\%ls", path);
    USTR name = { (USHORT)(wcslen(nt) * sizeof(WCHAR)), (USHORT)(wcslen(nt) * sizeof(WCHAR) + sizeof(WCHAR)), nt };
    OATTR attr = { sizeof attr, NULL, &name, 0x40 /* OBJ_CASE_INSENSITIVE */, NULL, NULL };
    ULONG access = STANDARD_RIGHTS_READ | SYNCHRONIZE | FILE_READ_ATTRIBUTES | FILE_READ_EA | FILE_TRAVERSE | FILE_LIST_DIRECTORY;
    ULONG options = 0x4000 /* FILE_OPEN_FOR_BACKUP_INTENT */ | 0x20 /* FILE_SYNCHRONOUS_IO_NONALERT */ | (directory_file ? 0x1 /* FILE_DIRECTORY_FILE */ : 0);
    HANDLE h = NULL;
    IOSB io;
    LONG status = NtCreateFile(&h, access, &attr, &io, NULL, FILE_ATTRIBUTE_NORMAL, 7, 1 /* FILE_OPEN */, options, NULL, 0);
    if (status == 0) CloseHandle(h);
    return status;
}

int wmain(int argc, wchar_t **argv) {
    if (argc < 3) { fwprintf(stderr, L"usage: probe WRITABLE_DIR READONLY_DIR\n"); return 2; }
    const wchar_t *rw = argv[1], *ro = argv[2];
    wchar_t a1[1024], a2[1024];
    swprintf(a1, 1024, L"%ls\\a1", rw); swprintf(a2, 1024, L"%ls\\a2", rw);
    touch(a1); touch(a2);
    wprintf(L"rename in a writable folder, by full path:            0x%08lX\n", (unsigned long)rename_file(a1, rw, L"b1", 0));
    wprintf(L"rename in a writable folder, relative to its handle:  0x%08lX\n", (unsigned long)rename_file(a2, rw, L"b2", 1));
    wprintf(L"open a read-only folder, without FILE_DIRECTORY_FILE: 0x%08lX\n", (unsigned long)open_dir(ro, 0));
    wprintf(L"open a read-only folder, with FILE_DIRECTORY_FILE:    0x%08lX\n", (unsigned long)open_dir(ro, 1));
    return 0;
}

Expected behavior

STATUS_SUCCESS for the rename relative to the directory handle, as outside the container:

rename in a writable folder, by full path:            0x00000000
rename in a writable folder, relative to its handle:  0x00000000
open a read-only folder, without FILE_DIRECTORY_FILE: 0x00000000
open a read-only folder, with FILE_DIRECTORY_FILE:    0x00000000

Actual behavior

Inside the container:

rename in a writable folder, by full path:            0x00000000
rename in a writable folder, relative to its handle:  0xC0000008
open a read-only folder, without FILE_DIRECTORY_FILE: 0x00000000
open a read-only folder, with FILE_DIRECTORY_FILE:    0xC0000022

The second line is this issue. FileRenameInformation (class 10) with RootDirectory set behaves the same as FileRenameInformationEx. The fourth line is #1467 (the probe's open asks for FILE_TRAVERSE and FILE_READ_EA with FILE_DIRECTORY_FILE), included because the same program meets both.

Possibly related: Git for Windows 2.55.0 inside the container does its work and then exits with the same 0xC0000008 when its output is a console (git --version), and exits 0 with its output redirected. Not narrowed down.

altohq avatar Nov 13 '26 21:11 altohq

Same exact issue on same build W11 26H2 26300.9974 with MXC 1.1.0 on dotnet SDK. Discovered this because bun install uses this operation and fails. I tried adding more permissions but I couldn't find a workaround.

AndreaVielCGN avatar Nov 18 '26 09:11 AndreaVielCGN