-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add socket-restrict gadget to block AF_ALG in containers #110
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
Merged
Merged
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
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
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,27 @@ | ||
| # socket-restrict | ||
|
|
||
| Restrict dangerous socket primitives in containers. | ||
|
|
||
| This gadget blocks all `AF_ALG` (kernel crypto userspace API) socket usage | ||
| inside containers. `AF_ALG` is rarely needed in containerized production | ||
| workloads — most TLS, SSH, and dm-crypt use cases never touch it — and | ||
| blocking it eliminates a class of kernel attack surface from the container | ||
| boundary. | ||
|
|
||
| The initial motivation is CVE-2026-31431 (Copy Fail), a Linux kernel local | ||
| privilege escalation in `algif_aead` that can be triggered via `AF_ALG` | ||
| sockets. This gadget blocks the entire killchain at socket creation time, | ||
| before any vulnerable kernel path is reached. | ||
|
|
||
| ## Hooks | ||
|
|
||
| | Hook | Purpose | | ||
| |---|---| | ||
| | `lsm/socket_create` | Block `AF_ALG` socket creation (main choke point) | | ||
| | `lsm/socket_bind` | Defense-in-depth: block `AF_ALG` bind if a socket FD exists from before policy load. Preserves `alg_type`/`alg_name` for visibility. | | ||
|
|
||
|
dorser marked this conversation as resolved.
|
||
| ## Getting Started | ||
|
|
||
| ```bash | ||
| sudo ig run ghcr.io/micromize-dev/micromize/gadgets/socket-restrict:latest | ||
| ``` | ||
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,55 @@ | ||
| name: socket-restrict | ||
| description: Restrict dangerous socket primitives in containers | ||
| homepageURL: https://github.com/micromize-dev/micromize/tree/main/gadgets/socket-restrict | ||
| documentationURL: https://github.com/micromize-dev/micromize/tree/main/gadgets/socket-restrict | ||
| sourceURL: https://github.com/micromize-dev/micromize/tree/main/gadgets/socket-restrict | ||
| datasources: | ||
| socket_restrict: | ||
| fields: | ||
| alg_name: | ||
| annotations: | ||
| description: The AF_ALG algorithm name requested by the process | ||
| alg_type: | ||
| annotations: | ||
| description: The AF_ALG algorithm type requested by the process | ||
| event_type: | ||
| annotations: | ||
| description: Numeric event type identifier | ||
| family: | ||
| annotations: | ||
| description: Socket address family | ||
| process: | ||
| annotations: | ||
| description: The process attempting a restricted socket operation | ||
| reason: | ||
| annotations: | ||
| description: Human-readable reason for the event | ||
| timestamp_raw: | ||
| annotations: | ||
| description: Timestamp of the event | ||
| params: | ||
| ebpf: | ||
| enforce: | ||
| key: enforce | ||
| defaultValue: "true" | ||
| description: Enforce the restriction (block) or just audit | ||
| targ_comm: | ||
| key: targ_comm | ||
| defaultValue: "" | ||
| description: Process name (comm) | ||
| targ_gid: | ||
| key: targ_gid | ||
| defaultValue: "" | ||
| description: Group ID | ||
| targ_pid: | ||
| key: targ_pid | ||
| defaultValue: "" | ||
| description: Process ID | ||
| targ_tid: | ||
| key: targ_tid | ||
| defaultValue: "" | ||
| description: Thread ID | ||
| targ_uid: | ||
| key: targ_uid | ||
| defaultValue: "" | ||
| description: User ID |
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,106 @@ | ||
| // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note | ||
| /* Copyright (c) 2024 micromize-Authors */ | ||
|
|
||
| #include "program.bpf.h" | ||
|
|
||
| #include <vmlinux.h> | ||
|
|
||
| #include <gadget/buffer.h> | ||
| #include <gadget/filter.h> | ||
| #include <gadget/macros.h> | ||
|
|
||
| const volatile int enforce = 1; | ||
| GADGET_PARAM(enforce); | ||
|
|
||
| GADGET_TRACER_MAP(events, 1024 * 256); | ||
|
|
||
| GADGET_TRACER(socket_restrict, events, event); | ||
|
|
||
| // Block AF_ALG socket creation — main choke point. | ||
| SEC("lsm/socket_create") | ||
| int BPF_PROG(micromize_socket_create, int family, int type, int protocol, | ||
| int kern) { | ||
| if (kern) | ||
| return 0; | ||
|
|
||
| if (gadget_should_discard_data_current()) | ||
| return 0; | ||
|
|
||
| if (family != AF_ALG) | ||
| return 0; | ||
|
|
||
| struct event *event; | ||
| event = gadget_reserve_buf(&events, sizeof(*event)); | ||
| if (!event) { | ||
| if (enforce) | ||
| return -EPERM; | ||
| return 0; | ||
| } | ||
|
|
||
| gadget_process_populate(&event->process); | ||
| event->timestamp_raw = bpf_ktime_get_boot_ns(); | ||
| event->event_type = EVENT_TYPE_SOCKET_AF_ALG_CREATE; | ||
| event->family = AF_ALG; | ||
| event->alg_type[0] = '\0'; | ||
| event->alg_name[0] = '\0'; | ||
|
|
||
| gadget_submit_buf(ctx, &events, event, sizeof(*event)); | ||
|
|
||
| if (enforce) | ||
| return -EPERM; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| // Defense-in-depth: block AF_ALG bind if a socket FD exists from before | ||
| // policy load. Preserves alg_type/alg_name for visibility. | ||
| SEC("lsm/socket_bind") | ||
| int BPF_PROG(micromize_socket_bind, struct socket *sock, | ||
| struct sockaddr *address, int addrlen) { | ||
| (void)sock; | ||
|
dorser marked this conversation as resolved.
|
||
|
|
||
| if (gadget_should_discard_data_current()) | ||
| return 0; | ||
|
|
||
| if (!address || addrlen < SOCKADDR_ALG_TYPE_END) | ||
| return 0; | ||
|
|
||
| __u16 family = 0; | ||
| bpf_probe_read_kernel(&family, sizeof(family), address); | ||
| if (family != AF_ALG) | ||
| return 0; | ||
|
|
||
| struct event *event; | ||
| event = gadget_reserve_buf(&events, sizeof(*event)); | ||
| if (!event) { | ||
| if (enforce) | ||
| return -EPERM; | ||
| return 0; | ||
| } | ||
|
|
||
| gadget_process_populate(&event->process); | ||
| event->timestamp_raw = bpf_ktime_get_boot_ns(); | ||
| event->event_type = EVENT_TYPE_SOCKET_AF_ALG_BIND; | ||
| event->family = family; | ||
|
|
||
| bpf_probe_read_kernel(event->alg_type, SOCKADDR_ALG_TYPE_LEN, | ||
| (const char *)address + SOCKADDR_ALG_TYPE_OFFSET); | ||
| event->alg_type[SOCKADDR_ALG_TYPE_LEN] = '\0'; | ||
|
|
||
| if (addrlen >= SOCKADDR_ALG_MIN_LEN) { | ||
| bpf_probe_read_kernel(event->alg_name, SOCKADDR_ALG_NAME_LEN, | ||
| (const char *)address + SOCKADDR_ALG_NAME_OFFSET); | ||
| event->alg_name[SOCKADDR_ALG_NAME_LEN - 1] = '\0'; | ||
|
dorser marked this conversation as resolved.
|
||
| } else { | ||
| event->alg_name[0] = '\0'; | ||
| } | ||
|
|
||
| gadget_submit_buf(ctx, &events, event, sizeof(*event)); | ||
|
|
||
| if (enforce) | ||
| return -EPERM; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| char LICENSE[] SEC("license") = "GPL"; | ||
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,32 @@ | ||
| // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note | ||
| /* Copyright (c) 2024 micromize-Authors */ | ||
|
|
||
| #include <gadget/common.h> | ||
| #include <micromize/event_types.h> | ||
|
|
||
| #ifndef EPERM | ||
| #define EPERM 1 | ||
| #endif | ||
|
|
||
| #ifndef AF_ALG | ||
| #define AF_ALG 38 | ||
| #endif | ||
|
|
||
| #define SOCKADDR_ALG_TYPE_OFFSET 2 | ||
| #define SOCKADDR_ALG_TYPE_LEN 14 | ||
| #define SOCKADDR_ALG_TYPE_END (SOCKADDR_ALG_TYPE_OFFSET + SOCKADDR_ALG_TYPE_LEN) | ||
|
|
||
| #define SOCKADDR_ALG_NAME_OFFSET 24 | ||
| #define SOCKADDR_ALG_NAME_LEN 64 | ||
| #define SOCKADDR_ALG_MIN_LEN (SOCKADDR_ALG_NAME_OFFSET + SOCKADDR_ALG_NAME_LEN) | ||
|
|
||
| #define EVENT_ALG_TYPE_LEN (SOCKADDR_ALG_TYPE_LEN + 1) | ||
|
|
||
| struct event { | ||
| gadget_timestamp timestamp_raw; | ||
| struct gadget_process process; | ||
| __u32 event_type; | ||
| __u32 family; | ||
| char alg_type[EVENT_ALG_TYPE_LEN]; | ||
| char alg_name[SOCKADDR_ALG_NAME_LEN]; | ||
| }; |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.