Skip to content

Commit 23bbba1

Browse files
Kui-Feng Leeyonghong-song
authored andcommitted
Implement bashreadline with libbpf.
Bashreadline will print user inputs, returning from readline, of every instance of bash shell. Readline is in bash itself, linked statically, for some devices, while others may link to libreadline.so. This implementation finds the symbol in bash if possible. Or, it tries to find libreadline.so using ldd if the symbol is not in bash. Signed-off-by: Kui-Feng Lee <[email protected]>
1 parent 00bb8e1 commit 23bbba1

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed

libbpf-tools/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.output
2+
/bashreadline
23
/bindsnoop
34
/biolatency
45
/biopattern

libbpf-tools/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $(error Architecture $(ARCH) is not supported yet. Please open an issue)
1616
endif
1717

1818
APPS = \
19+
bashreadline \
1920
bindsnoop \
2021
biolatency \
2122
biopattern \

libbpf-tools/bashreadline.bpf.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (c) 2021 Facebook */
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include "bashreadline.h"
7+
8+
#define TASK_COMM_LEN 16
9+
10+
struct {
11+
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
12+
__uint(key_size, sizeof(__u32));
13+
__uint(value_size, sizeof(__u32));
14+
} events SEC(".maps");
15+
16+
SEC("uretprobe/readline")
17+
int BPF_KRETPROBE(printret, const void *ret) {
18+
struct str_t data;
19+
char comm[TASK_COMM_LEN];
20+
u32 pid;
21+
22+
if (!ret)
23+
return 0;
24+
25+
bpf_get_current_comm(&comm, sizeof(comm));
26+
if (comm[0] != 'b' || comm[1] != 'a' || comm[2] != 's' || comm[3] != 'h' || comm[4] != 0 )
27+
return 0;
28+
29+
pid = bpf_get_current_pid_tgid() >> 32;
30+
data.pid = pid;
31+
bpf_probe_read_user_str(&data.str, sizeof(data.str), ret);
32+
33+
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &data, sizeof(data));
34+
35+
return 0;
36+
};
37+
38+
char LICENSE[] SEC("license") = "GPL";

libbpf-tools/bashreadline.c

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2+
/* Copyright (c) 2021 Facebook */
3+
#include <argp.h>
4+
#include <stdio.h>
5+
#include <errno.h>
6+
#include <signal.h>
7+
#include <time.h>
8+
#include <unistd.h>
9+
#include <ctype.h>
10+
11+
#include <bpf/libbpf.h>
12+
#include <bpf/bpf.h>
13+
#include "bashreadline.h"
14+
#include "bashreadline.skel.h"
15+
#include "trace_helpers.h"
16+
#include "uprobe_helpers.h"
17+
18+
#define PERF_BUFFER_PAGES 16
19+
#define PERF_POLL_TIMEOUT_MS 100
20+
#define warn(...) fprintf(stderr, __VA_ARGS__)
21+
22+
static volatile sig_atomic_t exiting = 0;
23+
24+
const char *argp_program_version = "bashreadline 1.0";
25+
const char *argp_program_bug_address =
26+
"https://github.com/iovisor/bcc/tree/master/libbpf-tools";
27+
const char argp_program_doc[] =
28+
"Print entered bash commands from all running shells.\n"
29+
"\n"
30+
"USAGE: bashreadline [-s <path/to/libreadline.so>]\n"
31+
"\n"
32+
"EXAMPLES:\n"
33+
" bashreadline\n"
34+
" bashreadline -s /usr/lib/libreadline.so\n";
35+
36+
static const struct argp_option opts[] = {
37+
{ "shared", 's', "PATH", 0, "the location of libreadline.so library" },
38+
{ NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
39+
{},
40+
};
41+
42+
static char *libreadline_path = NULL;
43+
44+
static error_t parse_arg(int key, char *arg, struct argp_state *state)
45+
{
46+
switch (key) {
47+
case 's':
48+
libreadline_path = strdup(arg);
49+
if (libreadline_path == NULL)
50+
return ARGP_ERR_UNKNOWN;
51+
break;
52+
case 'h':
53+
argp_state_help(state, stderr, ARGP_HELP_STD_HELP);
54+
break;
55+
default:
56+
return ARGP_ERR_UNKNOWN;
57+
}
58+
return 0;
59+
}
60+
61+
static void handle_event(void *ctx, int cpu, void *data, __u32 data_size)
62+
{
63+
struct str_t *e = data;
64+
struct tm *tm;
65+
char ts[16];
66+
time_t t;
67+
68+
time(&t);
69+
tm = localtime(&t);
70+
strftime(ts, sizeof(ts), "%H:%m:%S", tm);
71+
72+
printf("%-9s %-7d %s\n", ts, e->pid, e->str);
73+
}
74+
75+
static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
76+
{
77+
warn("lost %llu events on CPU #%d\n", lost_cnt, cpu);
78+
}
79+
80+
static char *find_readline_so()
81+
{
82+
const char *bash_path = "/bin/bash";
83+
FILE *fp;
84+
off_t func_off;
85+
char *line = NULL;
86+
size_t line_sz = 0;
87+
char path[128];
88+
char *result = NULL;
89+
90+
func_off = get_elf_func_offset(bash_path, "readline");
91+
if (func_off >= 0)
92+
return strdup(bash_path);
93+
94+
/*
95+
* Try to find libreadline.so if readline is not defined in
96+
* bash itself.
97+
*
98+
* ldd will print a list of names of shared objects,
99+
* dependencies, and their paths. The line for libreadline
100+
* would looks like
101+
*
102+
* libreadline.so.8 => /usr/lib/libreadline.so.8 (0x00007b....)
103+
*
104+
* Here, it finds a line with libreadline.so and extracts the
105+
* path after the arrow, '=>', symbol.
106+
*/
107+
fp = popen("ldd /bin/bash", "r");
108+
if (fp == NULL)
109+
goto cleanup;
110+
while (getline(&line, &line_sz, fp) >= 0) {
111+
if (sscanf(line, "%*s => %127s", path) < 1)
112+
continue;
113+
if (strstr(line, "/libreadline.so")) {
114+
result = strdup(path);
115+
break;
116+
}
117+
}
118+
119+
cleanup:
120+
if (line)
121+
free(line);
122+
if (fp)
123+
fclose(fp);
124+
return result;
125+
}
126+
127+
static void sig_int(int signo)
128+
{
129+
exiting = 1;
130+
}
131+
132+
int main(int argc, char **argv)
133+
{
134+
static const struct argp argp = {
135+
.options = opts,
136+
.parser = parse_arg,
137+
.doc = argp_program_doc,
138+
};
139+
struct bashreadline_bpf *obj = NULL;
140+
struct perf_buffer_opts pb_opts;
141+
struct perf_buffer *pb = NULL;
142+
char *readline_so_path;
143+
off_t func_off;
144+
int err;
145+
146+
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
147+
if (err)
148+
return err;
149+
150+
if (libreadline_path) {
151+
readline_so_path = libreadline_path;
152+
} else if ((readline_so_path = find_readline_so()) == NULL) {
153+
warn("failed to find readline\n");
154+
return 1;
155+
}
156+
157+
err = bump_memlock_rlimit();
158+
if (err) {
159+
warn("failed to increase rlimit: %d\n", err);
160+
goto cleanup;
161+
}
162+
163+
obj = bashreadline_bpf__open_and_load();
164+
if (!obj) {
165+
warn("failed to open and load BPF object\n");
166+
goto cleanup;
167+
}
168+
169+
func_off = get_elf_func_offset(readline_so_path, "readline");
170+
if (func_off < 0) {
171+
warn("cound not find readline in %s\n", readline_so_path);
172+
goto cleanup;
173+
}
174+
175+
obj->links.printret = bpf_program__attach_uprobe(obj->progs.printret, true, -1,
176+
readline_so_path, func_off);
177+
err = libbpf_get_error(obj->links.printret);
178+
if (err) {
179+
warn("failed to attach readline: %d\n", err);
180+
goto cleanup;
181+
}
182+
183+
pb_opts.sample_cb = handle_event;
184+
pb_opts.lost_cb = handle_lost_events;
185+
pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES, &pb_opts);
186+
err = libbpf_get_error(pb);
187+
if (err) {
188+
warn("failed to open perf buffer: %d\n", err);
189+
goto cleanup;
190+
}
191+
192+
if (signal(SIGINT, sig_int) == SIG_ERR) {
193+
warn("can't set signal handler: %s\n", strerror(errno));
194+
err = 1;
195+
goto cleanup;
196+
}
197+
198+
printf("%-9s %-7s %s\n", "TIME", "PID", "COMMAND");
199+
while (!exiting) {
200+
err = perf_buffer__poll(pb, PERF_POLL_TIMEOUT_MS);
201+
if (err < 0 && errno != EINTR) {
202+
warn("error polling perf buffer: %s\n", strerror(errno));
203+
goto cleanup;
204+
}
205+
err = 0;
206+
}
207+
208+
cleanup:
209+
if (readline_so_path)
210+
free(readline_so_path);
211+
perf_buffer__free(pb);
212+
bashreadline_bpf__destroy(obj);
213+
214+
return err != 0;
215+
}

libbpf-tools/bashreadline.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2+
/* Copyright (c) 2021 Facebook */
3+
#ifndef __BASHREADLINE_H
4+
#define __BASHREADLINE_H
5+
6+
#define MAX_LINE_SIZE 80
7+
8+
struct str_t {
9+
__u32 pid;
10+
char str[MAX_LINE_SIZE];
11+
};
12+
13+
#endif /* __BASHREADLINE_H */

0 commit comments

Comments
 (0)