Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,12 @@ if(Python3_Interpreter_FOUND)
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/ci/npu_decl_check.py --check --root ${CMAKE_SOURCE_DIR})
add_test(NAME npu_decl_check_self
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/ci/npu_decl_check.py --self-test)
# engine#2057: no fopen(..., "w"/"a") in the tree without a stated reason -- CodeQL's world-writable class, kept fixed
# by a check rather than by the next scan. Source text only: no build, no device.
add_test(NAME file_create_check
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/ci/file_create_check.py --check --root ${CMAKE_SOURCE_DIR})
add_test(NAME file_create_check_self
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/ci/file_create_check.py --self-test)
# the new-model watcher's alert contract (offline: synthetic batches against the
# committed registry, plus FastFlowLM's org listing and releases from the recorded
# payloads under tests/fixtures/fflm/ — engine #588), so a regression cannot leave the
Expand Down Expand Up @@ -1340,6 +1346,15 @@ target_include_directories(npu_pack_test PRIVATE npu/forward/include ${CMAKE_SOU
# NPU_INFER_DUMP_OPS (npu/forward/include/ops-dump.h, engine#1503): the on/off rule, the mean|x|/absmax arithmetic and
# the line's text. Header-only, no device. `add_dependencies(onebit ...)` is what gets it built in CI, whose target
# list is explicit (tools/ci/ci_targets_check.py).
# npu/forward/include/file_create.h (engine#2057): the openers every dump and the head-image cache write through -- 0600 /
# at most 0644 whatever the umask, an existing 0666 file narrowed, a symlink refused with ELOOP. Plain C, no model, no
# device; the call sites only swapped fopen() for an opener, so without this arm a revert to fopen() passes every test.
if(NOT WIN32)
add_executable(file_create_test tests/file_create_test.c)
target_include_directories(file_create_test PRIVATE npu/forward/include)
add_test(NAME file_create COMMAND file_create_test)
add_dependencies(onebit file_create_test)
endif()
add_executable(ops_dump_test tests/ops_dump_test.cpp)
target_include_directories(ops_dump_test PRIVATE npu/forward/include)
add_test(NAME ops_dump COMMAND ops_dump_test)
Expand Down
3 changes: 2 additions & 1 deletion app/forward_serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
// resets it from position 0.
#include "forward_serve.h"

#include "../npu/forward/include/file_create.h" // dumps are created 0600, never through a symlink
#include "npu_decode_note.h"

#include "../npu/forward/include/q4nx_forward.h"
Expand Down Expand Up @@ -536,7 +537,7 @@ void handle_generate_impl(ForwardEngine& e, const httplib::Request& req, httplib
if (!e.dump_logits.empty()) {
const std::string path = e.dump_logits + "/step_" + std::to_string(dump_req) + "_" +
std::to_string(n) + ".bin";
if (std::FILE* f = std::fopen(path.c_str(), "wb")) {
if (std::FILE* f = onebit_fopen_private_wb(path.c_str())) { // 0600, no symlink (file_create.h)
std::fwrite(logits.data(), sizeof(float), logits.size(), f);
std::fclose(f);
}
Expand Down
7 changes: 7 additions & 0 deletions docs/npu.md
Original file line number Diff line number Diff line change
Expand Up @@ -2544,6 +2544,13 @@ can fail a run.
variable and never sees that line is running a binary built before the dump existed (#1977): rebuild it, rather than
looking for a code path that skips the dump.

The files these dumps write (`NPU_INFER_DUMP_DIR`'s `layer_<ll>.f32`, `NPU_I8_GS_DUMP`'s weight-scale pairs) are
created owner-only (0600) and never through a symlink at the final path component, whatever the umask, and a dump file
an older build left at 0666 is narrowed to 0600 when it is rewritten; the head-image cache's files are at most 0644. Both go through `npu/forward/include/file_create.h`, so no write site uses `fopen(…, "w")`,
which creates with 0666 under the umask and follows a planted link (CodeQL `cpp/world-writable-file-creation`).
`tools/ci/file_create_check.py` refuses a new `fopen(…, "w")` or `"a"` anywhere in the tree unless the line says why
(`file-create-ok: <reason>`), and `tests/file_create_test.c` pins the modes.

```
[op] L00 rmsnorm rows=1 |x| mean 0.0143171 absmax 3.01935
[op] L00 ffn_gate_up rows=1 |x| mean 0.0258482 absmax 1.25728
Expand Down
97 changes: 97 additions & 0 deletions npu/forward/include/file_create.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2026 bong-water-water-bong
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// file_create.h -- create a file for writing with a stated mode, never world-writable, never through a symlink.
//
// fopen(path, "w") creates with 0666 & ~umask: world-writable under a permissive umask, and it follows a symlink that
// someone else planted at a predictable name. CodeQL reports every such site (cpp/world-writable-file-creation), and a
// PR that only touches the surrounding code inherits the alert as "new in code changed by this pull request" -- which
// is how #1830 sat red for a scan it did not cause. These two openers are what the env-gated dumps and the head-image
// cache use instead. The mode is a constant inside each function, so the scan can see it.
// onebit_fopen_private_wb 0600: dumps and probes (an activation or weight dump is nobody else's business)
// onebit_fopen_shared_wb 0644: the head-image cache, which a later serve reads back. A cache file only; anything
// that holds activations, weights or a request is a dump and takes the 0600 one.
// onebit_fopen_private_ab 0600, appending: a dump log several runs add to (ort_run's outputs.txt)
// The two `wb` openers truncate an existing file (the `ab` one appends); all three refuse a symlink at the final component (O_NOFOLLOW), and leave errno from the
// failing call, so a caller's strerror(errno) still names the open's reason. O_CREAT's mode applies only when the file
// is created, so a regular file that already exists -- one an older build wrote 0666 -- has every bit beyond the stated
// mode cleared with fchmod() as well. That only narrows: a stricter umask still wins (a 077 umask leaves the cache 0600),
// and a device or pseudo-file (/dev/full, /proc/...) keeps its own mode. Pinned by tests/file_create_test.c,
// and tools/ci/file_create_check.py refuses a new fopen(..., "w"/"a") anywhere in the tree.
// Plain C, so .c and .cpp sources share it.
#pragma once

#include <errno.h>
#include <stdio.h>
#if !defined(_WIN32)
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

#if !defined(_WIN32)
static inline FILE* onebit_fdopen_(int fd, mode_t mode, const char* how) {
if (fd < 0) return NULL;
struct stat st;
if (fstat(fd, &st) != 0) {
const int e = errno;
close(fd);
errno = e;
return NULL;
}
// Only ever narrows: bits beyond the stated mode are cleared, and none is added, so a stricter umask still wins.
if (S_ISREG(st.st_mode) && ((st.st_mode & 07777) & ~mode) != 0 && fchmod(fd, (st.st_mode & 07777) & mode) != 0) {
const int e = errno;
close(fd);
errno = e;
return NULL;
}
FILE* f = fdopen(fd, how);
if (!f) {
const int e = errno;
close(fd);
errno = e;
}
return f;
}
#endif

static inline FILE* onebit_fopen_private_wb(const char* path) {
#if defined(_WIN32)
return fopen(path, "wb"); // file-create-ok: Windows has no POSIX mode; the CodeQL finding is POSIX-only
#else
return onebit_fdopen_(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | S_IWUSR),
S_IRUSR | S_IWUSR, "wb");
#endif
}

static inline FILE* onebit_fopen_shared_wb(const char* path) {
#if defined(_WIN32)
return fopen(path, "wb"); // file-create-ok: Windows has no POSIX mode; the CodeQL finding is POSIX-only
#else
return onebit_fdopen_(
open(path, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, "wb");
#endif
}

static inline FILE* onebit_fopen_private_ab(const char* path) {
#if defined(_WIN32)
return fopen(path, "ab"); // file-create-ok: Windows has no POSIX mode; the CodeQL finding is POSIX-only
#else
return onebit_fdopen_(open(path, O_WRONLY | O_CREAT | O_APPEND | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | S_IWUSR),
S_IRUSR | S_IWUSR, "ab");
#endif
}
4 changes: 3 additions & 1 deletion npu/forward/include/residual-dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
#include <cstring>
#include <string>

#include "file_create.h" // the dump is created 0600, never through a symlink

namespace onebit::npu {

// Writes `count` floats to `path`. Returns false and fills `err` (path + strerror) when the file cannot be created, the
// write is short, or the close fails. `err` is left untouched on success.
inline bool write_residual_dump(const char * path, const float * data, size_t count, std::string * err) {
FILE * fp = std::fopen(path, "wb");
FILE * fp = onebit_fopen_private_wb(path);
if (fp == nullptr) {
if (err != nullptr) {
*err = std::string(path) + ": " + std::strerror(errno);
Expand Down
3 changes: 2 additions & 1 deletion npu/forward/src/q4nx_forward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "batch_width.h"
#include "ffn_cache_policy.h"
#include "q4nx_state_planes.h" // the plane-copy half of a snapshot, device-free and unit-tested
#include "file_create.h" // dumps are created 0600, never through a symlink
#include "model/op_dispatch.h" // the optional-op fallback seam (engine#803 A3)
#include <algorithm>
#include <fcntl.h>
Expand Down Expand Up @@ -3284,7 +3285,7 @@ void Q4nxNpuForward::host_gemm_i8(const float* A, int a_k, int M, int K, int N,
static int n = 0;
char path[4096];
std::snprintf(path, sizeof path, "%s/arm_L%d_N%d_K%d_%03d.bin", gdir, cur_layer_, N, K, n++);
if (std::FILE* f = std::fopen(path, "wb")) {
if (std::FILE* f = onebit_fopen_private_wb(path)) {
std::fwrite(t.d.data(), sizeof(float), std::min<size_t>((size_t)N, t.d.size()), f);
std::fclose(f);
}
Expand Down
3 changes: 2 additions & 1 deletion npu/forward/src/q4nx_head_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// geometry and a hash, and a reader that cannot reproduce the hash refuses instead of returning bytes it cannot vouch for.

#include "q4nx_head_image.h"
#include "file_create.h" /* the cache file is created 0644, never through a symlink */

#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -82,7 +83,7 @@ int q4nx_head_image_write(const char *path, const int8_t *codes, const float *sc
const int tn = snprintf(tmp, sizeof tmp, "%s.tmp", path);
#endif
if (tn <= 0 || (size_t)tn >= sizeof tmp) return 0;
FILE *f = fopen(tmp, "wb");
FILE *f = onebit_fopen_shared_wb(tmp);
if (!f) return 0;
int ok = fwrite(&h, sizeof h, 1, f) == 1 && fwrite(codes, (size_t)kd * (size_t)nd, 1, f) == 1 &&
fwrite(scales, (size_t)nd * sizeof(float), 1, f) == 1;
Expand Down
3 changes: 2 additions & 1 deletion npu/target/xdna/xdna_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// docs/npu-p4-target-split.md records what moved in A1 and what A2 (plan-driven setup) still owes.
#include "target/xdna/xdna_target.h"
#include "batch_width.h"
#include "file_create.h" // dumps are created 0600, never through a symlink

#include <algorithm>
#include <chrono>
Expand Down Expand Up @@ -1262,7 +1263,7 @@ void XdnaTarget::npu_gemm_rows(const GemvArgs& ar, std::vector<float>& out, std:
static int n = 0;
char path[4096];
std::snprintf(path, sizeof path, "%s/dev_L%d_N%d_K%d_%03d.bin", gdir, ar.layer, N, K, n++);
if (std::FILE* f = std::fopen(path, "wb")) {
if (std::FILE* f = onebit_fopen_private_wb(path)) {
if (gs && !gs->empty()) std::fwrite(gs->data(), sizeof(float), (size_t)N, f);
std::fclose(f);
}
Expand Down
2 changes: 1 addition & 1 deletion onnx/ep/onebit_ep_qdq_proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ long proc_kb(const char* key) {

// Writing 5 to clear_refs resets VmHWM, so the next run's peak is the run's own.
bool reset_peak() {
FILE* f = std::fopen("/proc/self/clear_refs", "w");
FILE* f = std::fopen("/proc/self/clear_refs", "w"); // file-create-ok: a /proc pseudo-file, never created
if (!f) return false;
const bool ok = std::fputc('5', f) != EOF;
std::fclose(f);
Expand Down
6 changes: 4 additions & 2 deletions onnx/ort_run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

#include <onnxruntime_c_api.h>

#include "../npu/forward/include/file_create.h" // dumps are created 0600, never through a symlink

#include <algorithm>
#include <cctype>
#include <chrono>
Expand Down Expand Up @@ -722,7 +724,7 @@ int run_generic(OrtSession* session, OrtMemoryInfo* mi, const Args& a) {
}

if (a.dump_all) ::mkdir(a.dump_all, 0700); // EEXIST is the normal case
FILE* side = a.dump_all ? std::fopen((std::string(a.dump_all) + "/outputs.txt").c_str(), "a") : nullptr;
FILE* side = a.dump_all ? onebit_fopen_private_ab((std::string(a.dump_all) + "/outputs.txt").c_str()) : nullptr;
for (size_t i = 0; i < outs.size(); ++i) {
OrtAllocator* alloc = nullptr;
char* gname = nullptr;
Expand Down Expand Up @@ -758,7 +760,7 @@ int run_generic(OrtSession* session, OrtMemoryInfo* mi, const Args& a) {
size_t elems = 1;
for (int64_t d : dims) elems *= (size_t)(d > 0 ? d : 1);
const size_t bytes = elems * elem_size(et);
FILE* f = std::fopen(path.c_str(), "wb");
FILE* f = onebit_fopen_private_wb(path.c_str());
if (!f) {
std::fprintf(stderr, "onebit-ort-run: cannot write %s\n", path.c_str());
rc = 2;
Expand Down
Loading
Loading