Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0a8a3e5b authored by Joel Fernandes's avatar Joel Fernandes
Browse files

BACKPORT: Add support for BPF_FUNC_probe_read_str



Downstream kernel is missing support for this. It is needed by I/O
readahead project to read strings from kernel memory on Pixel v4.9. Add
support for the same.

Bug: 135210418

Change-Id: I37895b395d3fd2637f1d69e111c4a6e2b8c0327a
Signed-off-by: default avatarJoel Fernandes <joelaf@google.com>
parent b8f2f4b8
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -81,6 +81,35 @@ static const struct bpf_func_proto bpf_probe_read_proto = {
	.arg3_type	= ARG_ANYTHING,
};

BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size, const void *, unsafe_ptr)
{
	int ret;

	/*
	 * The strncpy_from_unsafe() call will likely not fill the entire
	 * buffer, but that's okay in this circumstance as we're probing
	 * arbitrary memory anyway similar to bpf_probe_read() and might
	 * as well probe the stack. Thus, memory is explicitly cleared
	 * only in error case, so that improper users ignoring return
	 * code altogether don't copy garbage; otherwise length of string
	 * is returned that can be used for bpf_perf_event_output() et al.
	 */
	ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
	if (unlikely(ret < 0))
		memset(dst, 0, size);

	return ret;
}

static const struct bpf_func_proto bpf_probe_read_str_proto = {
	.func           = bpf_probe_read_str,
	.gpl_only       = true,
	.ret_type       = RET_INTEGER,
	.arg1_type	= ARG_PTR_TO_RAW_STACK,
	.arg2_type	= ARG_CONST_STACK_SIZE,
	.arg3_type	= ARG_ANYTHING,
};

BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
	   u32, size)
{
@@ -434,6 +463,8 @@ static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
		return &bpf_map_delete_elem_proto;
	case BPF_FUNC_probe_read:
		return &bpf_probe_read_proto;
	case BPF_FUNC_probe_read_str:
		return &bpf_probe_read_str_proto;
	case BPF_FUNC_ktime_get_ns:
		return &bpf_ktime_get_ns_proto;
	case BPF_FUNC_tail_call: