Skip to content

Commit 2e1b7bf

Browse files
bong-water-water-bongclaude
andcommitted
moe_trace: create the trace file 0644 and fail if it cannot be written
CodeQL cpp/world-writable-file-creation (alert 5): fopen("w") creates with 0666 and leaves the rest to the umask. Open with an explicit 0644, and stop with a message instead of writing through a null FILE* when the path is bad. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f346e69 commit 2e1b7bf

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎tools/moe_trace.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
#include <sstream>
5454
#include <string>
5555
#include <vector>
56+
#include <cerrno>
57+
#include <fcntl.h>
5658

5759
static std::map<int, std::vector<std::vector<int>>> g_cur; // layer -> token rows of the step running now
5860
static std::map<int, std::vector<float>> g_lout; // layer -> l_out of a one-token step
@@ -228,7 +230,11 @@ int main(int argc, char** argv) {
228230
if (!ctx) return 1;
229231
const llama_vocab* vocab = llama_model_get_vocab(model);
230232
auto* smpl = llama_sampler_init_greedy();
231-
Writer w; w.f = fopen(a[4], "w");
233+
Writer w;
234+
// the trace is data for the owner: 0644, not fopen's 0666 left to the umask
235+
const int tfd = open(a[4], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
236+
w.f = tfd < 0 ? nullptr : fdopen(tfd, "w");
237+
if (!w.f) { fprintf(stderr, "cannot write %s: %s\n", a[4], strerror(errno)); return 1; }
232238
int n_past = 0;
233239

234240
auto tokenize = [&](const std::string& text, bool bos) {

0 commit comments

Comments
 (0)