-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alan Jowett (from Dev Box) <[email protected]>
- Loading branch information
1 parent
dfe54b7
commit b273179
Showing
6 changed files
with
196 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,87 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// SPDX-License-Identifier: MIT | ||
|
||
// clang -O2 -Werror -c bindmonitor.c -o bindmonitor_jit.o | ||
// | ||
// For bpf code: clang -target bpf -O2 -Werror -c bindmonitor.c -o bindmonitor.o | ||
// this passes the checker | ||
|
||
// Whenever this sample program changes, bpf2c_tests will fail unless the | ||
// expected files in tests\bpf2c_tests\expected are updated. The following | ||
// script can be used to regenerate the expected files: | ||
// generate_expected_bpf2c_output.ps1 | ||
// | ||
// Usage: | ||
// .\scripts\generate_expected_bpf2c_output.ps1 <build_output_path> | ||
// Example: | ||
// .\scripts\generate_expected_bpf2c_output.ps1 .\x64\Debug\ | ||
#include "bpf_helpers.h" | ||
#include "ebpf_nethooks.h" | ||
|
||
typedef struct | ||
{ | ||
uint64_t parent_process_id; | ||
uint8_t command_line[256]; | ||
} proces_entry_t; | ||
|
||
struct | ||
{ | ||
__uint(type, BPF_MAP_TYPE_HASH); | ||
__type(key, uint64_t); | ||
__type(value, proces_entry_t); | ||
__uint(max_entries, 1024); | ||
} process_map SEC(".maps"); | ||
|
||
// For debug builds, limit the number of iterations in the loop to 16 to prevent the verifier from | ||
// running for too long. For release builds, limit the number of iterations to 256. | ||
#if defined(NDEBUG) | ||
#define BOUNDED_MEMCPY_LIMIT 256 | ||
#else | ||
#define BOUNDED_MEMCPY_LIMIT 16 | ||
#endif | ||
|
||
__attribute__((always_inline)) | ||
// Copy the first 'source_size' bytes from 'source' to 'destination' and bound the copy to 'destination_size' bytes. | ||
void | ||
bounded_memcpy(uint8_t* destination, const uint8_t* source, uint32_t destination_size, uint32_t source_size) | ||
{ | ||
// Prevail verifier doesn't correctly compute the number of iterations in the loop. | ||
// Unroll the loop to avoid the verifier error. | ||
// Issue: #<to be determined> | ||
#pragma unroll | ||
for (uint32_t index = 0; index < BOUNDED_MEMCPY_LIMIT; index++) { | ||
if (index < destination_size && index < source_size) { | ||
destination[index] = source[index]; | ||
} | ||
} | ||
} | ||
|
||
// The following line is optional, but is used to verify | ||
// that the ProcesMonitor prototype is correct or the compiler | ||
// would complain when the function is actually defined below. | ||
process_hook_t ProcesMonitor; | ||
|
||
SEC("process") | ||
int | ||
ProcessMonitor(process_md_t* ctx) | ||
{ | ||
if (ctx->operation == PROCESS_OPERATION_CREATE) { | ||
proces_entry_t entry; | ||
__builtin_memset(&entry, 0, sizeof(entry)); | ||
entry.parent_process_id = ctx->parent_process_id; | ||
uint64_t process_id = ctx->process_id; | ||
|
||
bounded_memcpy( | ||
entry.command_line, | ||
ctx->command_start, | ||
sizeof(entry.command_line), | ||
(uint32_t)(ctx->command_end - ctx->command_start)); | ||
|
||
bpf_map_update_elem(&process_map, &process_id, &entry, BPF_ANY); | ||
} else if (ctx->operation == PROCESS_OPERATION_DELETE) { | ||
uint64_t process_id = ctx->process_id; | ||
bpf_map_delete_elem(&process_map, &process_id); | ||
} | ||
return 0; | ||
} |
This file contains 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