Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/30-sslsniff/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,17 @@ In the provided code snippet, based on the setting of the `env` environment vari

To achieve this functionality, the `find_library_path` function is first used to determine the library's path. Then, depending on the library type, the corresponding `attach_` function is called to attach the eBPF program to the library function. If `find_library_path` cannot locate a library (for example, because it is not installed on the system), the tool prints a warning to stderr and skips probing that provider instead of attaching against an invalid path. Each successful attachment is tracked separately so that multiple libraries can share the same eBPF program without losing link handles. Incompatible provider symbols produce a warning, and the tool exits if no probe could be attached.

`find_library_path` resolves names through `ldconfig`, so it only sees libraries in the loader cache. For a `libssl.so.1.1` from a custom prefix, or a `libnspr4.so` bundled inside an application directory, `--extra-lib PROVIDER:PATH` attaches to the given path with the same probe set as the matching built-in provider. The path is used as given, and a failure to attach is fatal rather than silently ignored.

```c
attach_provider(obj, env.openssl, "OpenSSL", "libssl.so", attach_openssl);
attach_provider(obj, env.gnutls, "GnuTLS", "libgnutls.so", attach_gnutls);
attach_provider(obj, env.nss, "NSS", "libnspr4.so", attach_nss);
if (env.extra_lib) {
err = attach_extra_lib(obj, env.extra_lib);
if (err)
goto cleanup;
}
```

This section primarily covers the attachment logic for the OpenSSL, GnuTLS, and NSS libraries. NSS is a set of security libraries designed for organizations, supporting the creation of secure client and server applications. Originally developed by Netscape, they are now maintained by Mozilla. The other two libraries have been introduced earlier and are not reiterated here.
Expand Down
7 changes: 7 additions & 0 deletions src/30-sslsniff/README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,17 @@ int BPF_URETPROBE(probe_SSL_do_handshake_exit) {

为了实现这一功能,首先利用 `find_library_path` 函数确定库的路径。然后,根据库的类型,调用对应的 `attach_` 函数来将 eBPF 程序挂载到库函数上。如果 `find_library_path` 找不到某个库(例如系统中没有安装该库),工具会向 stderr 打印一条警告并跳过该库的探测,而不是挂载到无效的路径上。每次成功挂载都会单独保存 link 句柄,因此多个库可以复用同一个 eBPF 程序而不会丢失句柄;库符号不兼容时会给出警告,如果一个探针都未能挂载,工具会退出。

`find_library_path` 通过 `ldconfig` 解析库名,因此只能看到加载器缓存中的库。对于安装在自定义前缀下的 `libssl.so.1.1`,或者随应用目录一起分发的 `libnspr4.so`,可以用 `--extra-lib PROVIDER:PATH` 按给定路径挂载,并使用与对应内置库相同的探针集合。路径按原样使用,挂载失败会直接报错退出,而不是静默忽略。

```c
attach_provider(obj, env.openssl, "OpenSSL", "libssl.so", attach_openssl);
attach_provider(obj, env.gnutls, "GnuTLS", "libgnutls.so", attach_gnutls);
attach_provider(obj, env.nss, "NSS", "libnspr4.so", attach_nss);
if (env.extra_lib) {
err = attach_extra_lib(obj, env.extra_lib);
if (err)
goto cleanup;
}
```

这里主要包含 OpenSSL、GnuTLS 和 NSS 三个库的挂载逻辑。NSS 是为组织设计的一套安全库,支持创建安全的客户端和服务器应用程序。它们最初是由 Netscape 开发的,现在由 Mozilla 维护。其他两个库前面已经介绍过了,这里不再赘述。
Expand Down
96 changes: 96 additions & 0 deletions src/30-sslsniff/sslsniff.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
static size_t attached_link_count;
static int track_attached_link(struct bpf_link *link, const char *program_name);
static char *find_library_path(const char *libname);
int attach_openssl(struct sslsniff_bpf *skel, const char *lib);
int attach_gnutls(struct sslsniff_bpf *skel, const char *lib);
int attach_nss(struct sslsniff_bpf *skel, const char *lib);

#define __ATTACH_UPROBE(skel, binary_path, sym_name, prog_name, is_retprobe) \
do { \
Expand Down Expand Up @@ -112,14 +115,55 @@
{"latency", 'l', NULL, 0, "Show function latency"},
{"handshake", HANDSHAKE_KEY, NULL, 0,
"Show SSL handshake latency, enabled only if latency option is on."},
{"extra-lib", EXTRA_LIB_KEY, "PROVIDER:PATH", 0,
"Also attach to this library, e.g. openssl:/opt/lib/libssl.so.1.1"},
{"verbose", 'v', NULL, 0, "Verbose debug output"},
{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
{},
};

static bool verbose = false;

struct ssl_provider {
const char *name;
int (*attach)(struct sslsniff_bpf *skel, const char *lib);
};

static const struct ssl_provider providers[] = {
{"openssl", attach_openssl},
{"gnutls", attach_gnutls},
{"nss", attach_nss},
};

/* Split a PROVIDER:PATH spec for --extra-lib; both halves point into spec.
* This is the only place the spec grammar lives, so option parsing and attach
* cannot disagree about what is accepted. *why describes -EINVAL. */
static int parse_extra_lib(const char *spec,
const struct ssl_provider **provider, const char **path,
const char **why) {
const char *sep = strchr(spec, ':');
size_t name_len;

if (!sep || sep == spec || sep[1] == '\0') {
*why = "expected PROVIDER:PATH";
return -EINVAL;
}
name_len = (size_t)(sep - spec);
for (size_t i = 0; i < sizeof(providers) / sizeof(providers[0]); i++) {
if (strlen(providers[i].name) == name_len &&
strncmp(providers[i].name, spec, name_len) == 0) {
if (provider)
*provider = &providers[i];
if (path)
*path = sep + 1;
return 0;
}
}
*why = "provider must be openssl, gnutls or nss";
return -EINVAL;
}

static error_t parse_arg(int key, char *arg, struct argp_state *state) {

Check notice on line 166 in src/30-sslsniff/sslsniff.c

View check run for this annotation

codefactor.io / CodeFactor

Complex Method

Complexity is 18 (lines 166-227).
switch (key) {
case 'p':
env.pid = atoi(arg);
Expand Down Expand Up @@ -154,6 +198,20 @@
case HANDSHAKE_KEY:
env.handshake = true;
break;
case EXTRA_LIB_KEY: {
const char *why = NULL;

// Report a bad value through argp like every other parse failure,
// instead of exit()ing from inside the callback.
if (parse_extra_lib(arg, NULL, NULL, &why) != 0)
argp_error(state, "invalid --extra-lib `%s`: %s", arg,
why);
env.extra_lib = strdup(arg);
if (!env.extra_lib)
argp_failure(state, EXIT_FAILURE, ENOMEM,
"cannot duplicate --extra-lib value");
break;
}
case 'h':
argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
break;
Expand Down Expand Up @@ -269,6 +327,39 @@
warn("%s probing is incomplete\n", name);
}

/*
* Attach to a user-specified library given as PROVIDER:PATH, where PROVIDER
* selects which symbol set to hook with the same probes the built-in providers
* use. This exists because find_library_path() resolves through ldconfig,
* which does not know about libraries outside the loader cache - a
* distribution's libssl from a custom prefix, or a bundled NSS inside an
* application directory.
*/
static int attach_extra_lib(struct sslsniff_bpf *obj, const char *spec) {
const struct ssl_provider *provider = NULL;
const char *path = NULL, *why = NULL;
size_t attached_before = attached_link_count;
int err = parse_extra_lib(spec, &provider, &path, &why);

if (err) {
warn("invalid --extra-lib `%s`: %s\n", spec, why);
return err;
}

// The path is used as given: libbpf opens the library itself and
// reports the precise reason if it cannot, so there is no check-then-
// open window and no real/effective UID mismatch under sudo.
printf("extra %s path: %s\n", provider->name, path);
err = provider->attach(obj, path);
if (attached_link_count == attached_before) {
warn("%s probing of %s failed\n", provider->name, path);
return err ? err : -ENOENT;
}
if (err)
warn("%s probing of %s is incomplete\n", provider->name, path);
return 0;
}

/*
* Find the path of a library using ldconfig.
*/
Expand Down Expand Up @@ -438,6 +529,11 @@
attach_provider(obj, env.openssl, "OpenSSL", "libssl.so", attach_openssl);
attach_provider(obj, env.gnutls, "GnuTLS", "libgnutls.so", attach_gnutls);
attach_provider(obj, env.nss, "NSS", "libnspr4.so", attach_nss);
if (env.extra_lib) {
err = attach_extra_lib(obj, env.extra_lib);
if (err)
goto cleanup;
}
if (attached_link_count == 0) {
warn("no SSL provider probes attached\n");
err = -ENOENT;
Expand Down