Skip to content

Commit 6c0c990

Browse files
committed
permission: enforce fs write permission for trace events
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: nodejs-private/node-private#927 CVE-ID: CVE-2026-56847
1 parent 34ed88a commit 6c0c990

9 files changed

Lines changed: 1186 additions & 13 deletions

File tree

src/node_trace_events.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include "node_external_reference.h"
66
#include "node_internals.h"
77
#include "node_v8_platform-inl.h"
8+
#include "permission/permission.h"
89
#include "tracing/agent.h"
10+
#include "tracing/node_trace_writer.h"
911
#include "util-inl.h"
1012

1113
#include <set>
@@ -83,6 +85,12 @@ void NodeCategorySet::Enable(const FunctionCallbackInfo<Value>& args) {
8385
CHECK_NOT_NULL(category_set);
8486
const auto& categories = category_set->GetCategories();
8587
if (!category_set->enabled_ && !categories.empty()) {
88+
const std::string filepath = tracing::NodeTraceWriter::GetFilePath(
89+
per_process::cli_options->trace_event_file_pattern, 1);
90+
THROW_IF_INSUFFICIENT_PERMISSIONS(
91+
category_set->env(),
92+
permission::PermissionScope::kFileSystemWrite,
93+
filepath);
8694
// Starts the Tracing Agent if it wasn't started already (e.g. through
8795
// a command line flag.)
8896
StartTracingAgent();

src/tracing/node_trace_writer.cc

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,27 @@
88
namespace node {
99
namespace tracing {
1010

11+
void replace_substring(std::string* target,
12+
const std::string& search,
13+
const std::string& insert) {
14+
size_t pos = target->find(search);
15+
for (; pos != std::string::npos; pos = target->find(search, pos)) {
16+
target->replace(pos, search.size(), insert);
17+
pos += insert.size();
18+
}
19+
}
20+
1121
NodeTraceWriter::NodeTraceWriter(const std::string& log_file_pattern)
1222
: log_file_pattern_(log_file_pattern) {}
1323

24+
std::string NodeTraceWriter::GetFilePath(const std::string& log_file_pattern,
25+
int file_num) {
26+
std::string filepath(log_file_pattern);
27+
replace_substring(&filepath, "${pid}", std::to_string(uv_os_getpid()));
28+
replace_substring(&filepath, "${rotation}", std::to_string(file_num));
29+
return filepath;
30+
}
31+
1432
void NodeTraceWriter::InitializeOnThread(uv_loop_t* loop) {
1533
CHECK_NULL(tracing_loop_);
1634
tracing_loop_ = loop;
@@ -60,25 +78,13 @@ NodeTraceWriter::~NodeTraceWriter() {
6078
}
6179
}
6280

63-
void replace_substring(std::string* target,
64-
const std::string& search,
65-
const std::string& insert) {
66-
size_t pos = target->find(search);
67-
for (; pos != std::string::npos; pos = target->find(search, pos)) {
68-
target->replace(pos, search.size(), insert);
69-
pos += insert.size();
70-
}
71-
}
72-
7381
void NodeTraceWriter::OpenNewFileForStreaming() {
7482
++file_num_;
7583
uv_fs_t req;
7684

7785
// Evaluate a JS-style template string, it accepts the values ${pid} and
7886
// ${rotation}
79-
std::string filepath(log_file_pattern_);
80-
replace_substring(&filepath, "${pid}", std::to_string(uv_os_getpid()));
81-
replace_substring(&filepath, "${rotation}", std::to_string(file_num_));
87+
std::string filepath(GetFilePath(log_file_pattern_, file_num_));
8288

8389
if (fd_ != -1) {
8490
CHECK_EQ(uv_fs_close(nullptr, &req, fd_, nullptr), 0);

src/tracing/node_trace_writer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class NodeTraceWriter : public AsyncTraceWriter {
1919
explicit NodeTraceWriter(const std::string& log_file_pattern);
2020
~NodeTraceWriter() override;
2121

22+
static std::string GetFilePath(const std::string& log_file_pattern,
23+
int file_num);
24+
2225
void InitializeOnThread(uv_loop_t* loop) override;
2326
void AppendTraceEvent(TraceObject* trace_event) override;
2427
void Flush(bool blocking) override;

0 commit comments

Comments
 (0)