-
Couldn't load subscription status.
- Fork 8
src: add GetBuildId helper function #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
santigimeno
wants to merge
1
commit into
node-v22.x-nsolid-v6.x
Choose a base branch
from
santi/add_build_id
base: node-v22.x-nsolid-v6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #include "nsolid_elf_utils.h" | ||
| #include "nsolid_util.h" | ||
|
|
||
| #include <fcntl.h> | ||
| #include <libelf.h> | ||
| #include <gelf.h> | ||
| #include <unistd.h> | ||
| #include <cstring> | ||
| #include <map> | ||
| #include "uv.h" | ||
|
|
||
| namespace node { | ||
| namespace nsolid { | ||
| namespace elf_utils { | ||
|
|
||
|
|
||
| int GetBuildId(const std::string& path, std::string* build_id) { | ||
| static std::map<std::string, std::string> build_id_cache_; | ||
|
|
||
| Elf* e; | ||
| Elf_Scn* scn = nullptr; | ||
| GElf_Shdr shdr; | ||
|
|
||
| if (build_id_cache_.find(path) != build_id_cache_.end()) { | ||
| *build_id = build_id_cache_[path]; | ||
| return 0; | ||
| } | ||
|
|
||
| int ret; | ||
|
|
||
| ret = 0; | ||
| if (elf_version(EV_CURRENT) == EV_NONE) { | ||
| return elf_errno(); | ||
| } | ||
|
|
||
| int fd = open(path.c_str(), O_RDONLY); | ||
| if (fd < 0) { | ||
| return -errno; | ||
| } | ||
|
|
||
| e = elf_begin(fd, ELF_C_READ, nullptr); | ||
| if (!e) { | ||
| ret = elf_errno(); | ||
| goto error; | ||
| } | ||
|
|
||
| size_t shstrndx; | ||
| if (elf_getshdrstrndx(e, &shstrndx) != 0) { | ||
| ret = elf_errno(); | ||
| goto end_error; | ||
| } | ||
|
|
||
| *build_id = std::string(""); | ||
| while ((scn = elf_nextscn(e, scn)) != nullptr) { | ||
| if (gelf_getshdr(scn, &shdr) != &shdr) { | ||
| ret = elf_errno(); | ||
| goto end_error; | ||
| } | ||
|
|
||
| char* name = elf_strptr(e, shstrndx, shdr.sh_name); | ||
| if (name && strcmp(name, ".note.gnu.build-id") == 0) { | ||
| Elf_Data* data = elf_getdata(scn, nullptr); | ||
| if (data && data->d_size >= 16) { | ||
| // ELF Note header: namesz(4), descsz(4), type(4) + name padding | ||
| // Compute offset to build-id properly | ||
| uint32_t* note = reinterpret_cast<uint32_t*>(data->d_buf); | ||
| uint32_t namesz = note[0]; | ||
| uint32_t descsz = note[1]; | ||
| // Name starts at offset 12 | ||
| // Descriptor (build-id) starts at next aligned offset | ||
| size_t name_end = 12 + ((namesz + 3) & ~3); | ||
| uint8_t* id = reinterpret_cast<uint8_t*>(data->d_buf) + name_end; | ||
| *build_id = utils::buffer_to_hex(id, descsz); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (build_id->empty()) { | ||
| ret = UV_ENOENT; | ||
| } else { | ||
| build_id_cache_[path] = *build_id; | ||
| } | ||
|
|
||
| end_error: | ||
| elf_end(e); | ||
|
|
||
| error: | ||
| close(fd); | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| } // namespace elf_utils | ||
| } // namespace nsolid | ||
| } // namespace node | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef SRC_NSOLID_NSOLID_ELF_UTILS_H_ | ||
| #define SRC_NSOLID_NSOLID_ELF_UTILS_H_ | ||
|
|
||
| #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace node { | ||
| namespace nsolid { | ||
|
|
||
| namespace elf_utils { | ||
| int GetBuildId(const std::string& path, std::string* build_id); | ||
| } // namespace elf_utils | ||
| } // namespace nsolid | ||
| } // namespace node | ||
|
|
||
| #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
|
||
| #endif // SRC_NSOLID_NSOLID_ELF_UTILS_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #include <node.h> | ||
| #include <v8.h> | ||
| #include <cassert> | ||
| #include <string> | ||
| #if defined(__linux__) | ||
| #include "../../../src/nsolid/nsolid_elf_utils.h" | ||
| #endif | ||
|
|
||
| using v8::FunctionCallbackInfo; | ||
| using v8::Isolate; | ||
| using v8::String; | ||
| using v8::Value; | ||
|
|
||
| static void GetBuildId(const FunctionCallbackInfo<Value>& args) { | ||
| #if defined(__linux__) | ||
| Isolate* isolate = args.GetIsolate(); | ||
| assert(args[0]->IsString()); | ||
| v8::String::Utf8Value path_utf8(isolate, args[0]); | ||
| std::string path(*path_utf8, path_utf8.length()); | ||
| std::string build_id; | ||
| int res = node::nsolid::elf_utils::GetBuildId(path, &build_id); | ||
| if (res != 0) { | ||
| return; | ||
| } | ||
|
|
||
| args.GetReturnValue().Set( | ||
| String::NewFromUtf8(isolate, build_id.c_str()).ToLocalChecked()); | ||
| #endif | ||
| } | ||
|
|
||
| NODE_MODULE_INIT(/* exports, module, context */) { | ||
| NODE_SET_METHOD(exports, "getBuildId", GetBuildId); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| 'targets': [ | ||
| { | ||
| 'target_name': 'binding', | ||
| 'sources': [ 'binding.cc' ], | ||
| 'includes': ['../common.gypi'], | ||
| 'defines': [ 'NODE_WANT_INTERNALS=1' ], | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 'use strict'; | ||
| const common = require('../../common'); | ||
| const assert = require('assert'); | ||
| const { execSync } = require('child_process'); | ||
| const process = require('process'); | ||
|
|
||
| // Only run on Linux | ||
| if (process.platform !== 'linux') { | ||
| console.log('Skipping: nsolid-elf-utils only supported on Linux'); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const bindingPath = require.resolve(`./build/${common.buildType}/binding`); | ||
| const binding = require(bindingPath); | ||
|
|
||
| const expected = | ||
| execSync(`readelf -n ${process.execPath} | awk '/Build ID/ { print $3 }'`, | ||
| { encoding: 'utf8' }).trim(); | ||
|
|
||
| const buildId = binding.getBuildId(process.execPath); | ||
| assert.strictEqual(buildId, | ||
| expected, | ||
| `Mismatch: addon='${buildId}', readelf='${expected}'`); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential buffer overflow risk in ELF note parsing.
The code assumes the ELF note structure without validating that the computed offsets don't exceed the buffer bounds. While
data->d_size >= 16checks for minimum size, it doesn't ensure thatname_end + descszdoesn't exceeddata->d_size.Add bounds checking before accessing the build-id data:
*build_id = std::string(""); while ((scn = elf_nextscn(e, scn)) != nullptr) { if (gelf_getshdr(scn, &shdr) != &shdr) { ret = elf_errno(); goto end_error; } char* name = elf_strptr(e, shstrndx, shdr.sh_name); if (name && strcmp(name, ".note.gnu.build-id") == 0) { Elf_Data* data = elf_getdata(scn, nullptr); if (data && data->d_size >= 16) { // ELF Note header: namesz(4), descsz(4), type(4) + name padding // Compute offset to build-id properly uint32_t* note = reinterpret_cast<uint32_t*>(data->d_buf); uint32_t namesz = note[0]; uint32_t descsz = note[1]; // Name starts at offset 12 // Descriptor (build-id) starts at next aligned offset size_t name_end = 12 + ((namesz + 3) & ~3); + // Ensure we don't read beyond the buffer + if (name_end + descsz > data->d_size) { + ret = ELF_E_DATA; + goto end_error; + } uint8_t* id = reinterpret_cast<uint8_t*>(data->d_buf) + name_end; *build_id = utils::buffer_to_hex(id, descsz); break; } } }📝 Committable suggestion
🤖 Prompt for AI Agents