Skip to content

Commit 1e94acb

Browse files
dthalerAlan-Jowett
authored and
Alan Jowett
committed
Fix port_quota app usage (microsoft#1184)
* port quota Signed-off-by: Dave Thaler <[email protected]> * Fix port_quota load Signed-off-by: Dave Thaler <[email protected]> Co-authored-by: Alan Jowett <[email protected]>
1 parent e870a29 commit 1e94acb

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

external/ebpf-verifier

libs/execution_context/ebpf_core.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,14 @@ ebpf_core_resolve_maps(
353353
{
354354
EBPF_LOG_ENTRY();
355355
ebpf_program_t* program = NULL;
356-
memset(map_addresses, 0, sizeof(uintptr_t) * count_of_maps);
356+
uint32_t map_index = 0;
357357

358358
ebpf_result_t return_value =
359359
ebpf_reference_object_by_handle(program_handle, EBPF_OBJECT_PROGRAM, (ebpf_core_object_t**)&program);
360360
if (return_value != EBPF_SUCCESS)
361361
goto Done;
362362

363-
for (uint32_t map_index = 0; map_index < count_of_maps; map_index++) {
363+
for (map_index = 0; map_index < count_of_maps; map_index++) {
364364
ebpf_map_t* map;
365365
return_value =
366366
ebpf_reference_object_by_handle(map_handles[map_index], EBPF_OBJECT_MAP, (ebpf_core_object_t**)&map);
@@ -375,10 +375,8 @@ ebpf_core_resolve_maps(
375375

376376
Done:
377377
// Release our reference only after the map has been associated with the program.
378-
for (uint32_t map_index = 0; map_index < count_of_maps; map_index++) {
379-
if (map_addresses[map_index]) {
380-
ebpf_object_release_reference((ebpf_core_object_t*)map_addresses[map_index]);
381-
}
378+
for (uint32_t map_index2 = 0; map_index2 < map_index; map_index2++) {
379+
ebpf_object_release_reference((ebpf_core_object_t*)map_addresses[map_index2]);
382380
}
383381

384382
ebpf_object_release_reference((ebpf_core_object_t*)program);

scripts/deploy-ebpf.ps1

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ $build_directory=".\x64\Debug"
6868
"droppacket_unsafe.o",
6969
"EbpfApi.pdb",
7070
"ebpfnetsh.pdb",
71+
"ebpfsvc.pdb",
7172
"encap_reflect_packet.o",
7273
"encap_reflect_packet_um.dll",
7374
"encap_reflect_packet_um.pdb",
@@ -94,6 +95,8 @@ $build_directory=".\x64\Debug"
9495
"map.sys",
9596
"pidtgid.o",
9697
"pidtgid.sys",
98+
"port_quota.exe",
99+
"port_quota.pdb",
97100
"printk.o",
98101
"printk_legacy.o",
99102
"printk_legacy_um.dll",

tests/end_to_end/test_helper.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ GlueDeviceIoControl(
344344
size_t minimum_request_size = 0;
345345
size_t minimum_reply_size = 0;
346346
bool async = false;
347+
DWORD sharedBufferSize = (nInBufferSize > nOutBufferSize) ? nInBufferSize : nOutBufferSize;
348+
std::vector<uint8_t> sharedBuffer(sharedBufferSize);
347349

348350
result = ebpf_core_get_protocol_handler_properties(request_id, &minimum_request_size, &minimum_reply_size, &async);
349351
if (result != EBPF_SUCCESS)
@@ -369,15 +371,24 @@ GlueDeviceIoControl(
369371
// Intercept the call to perform any IOCTL specific _pre_ tasks.
370372
_preprocess_ioctl(user_request);
371373

374+
// In the kernel execution context, the request and reply share
375+
// the same memory. So to catch bugs that only show up in that
376+
// case, we force the same here.
377+
memcpy(sharedBuffer.data(), user_request, nInBufferSize);
378+
372379
result = ebpf_core_invoke_protocol_handler(
373380
request_id,
374-
user_request,
381+
sharedBuffer.data(),
375382
static_cast<uint16_t>(nInBufferSize),
376-
user_reply,
383+
(minimum_reply_size > 0) ? sharedBuffer.data() : nullptr,
377384
static_cast<uint16_t>(nOutBufferSize),
378385
lpOverlapped,
379386
_complete_overlapped);
380387

388+
if (minimum_reply_size > 0) {
389+
memcpy(user_reply, sharedBuffer.data(), nOutBufferSize);
390+
}
391+
381392
if (result != EBPF_SUCCESS)
382393
goto Fail;
383394

tools/port_quota/port_quota.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ load(int argc, char** argv)
3737
return 1;
3838
}
3939

40-
ebpf_object_set_execution_type(object, EBPF_EXECUTION_INTERPRET);
40+
ebpf_object_set_execution_type(object, EBPF_EXECUTION_JIT);
4141
program = bpf_object__next_program(object, nullptr);
4242
if (bpf_object__load(object) < 0) {
4343
fprintf(stderr, "Failed to load port quota eBPF program\n");
@@ -202,4 +202,4 @@ main(int argc, char** argv)
202202
}
203203
print_usage(argv[0]);
204204
return 1;
205-
}
205+
}

0 commit comments

Comments
 (0)