Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <test_progs.h>
0003 
0004 /* TODO: corrupts other tests uses connect() */
0005 void serial_test_probe_user(void)
0006 {
0007     static const char *const prog_names[] = {
0008         "handle_sys_connect",
0009 #if defined(__s390x__)
0010         "handle_sys_socketcall",
0011 #endif
0012     };
0013     enum { prog_count = ARRAY_SIZE(prog_names) };
0014     const char *obj_file = "./test_probe_user.o";
0015     DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, );
0016     int err, results_map_fd, sock_fd, duration = 0;
0017     struct sockaddr curr, orig, tmp;
0018     struct sockaddr_in *in = (struct sockaddr_in *)&curr;
0019     struct bpf_link *kprobe_links[prog_count] = {};
0020     struct bpf_program *kprobe_progs[prog_count];
0021     struct bpf_object *obj;
0022     static const int zero = 0;
0023     size_t i;
0024 
0025     obj = bpf_object__open_file(obj_file, &opts);
0026     if (!ASSERT_OK_PTR(obj, "obj_open_file"))
0027         return;
0028 
0029     for (i = 0; i < prog_count; i++) {
0030         kprobe_progs[i] =
0031             bpf_object__find_program_by_name(obj, prog_names[i]);
0032         if (CHECK(!kprobe_progs[i], "find_probe",
0033               "prog '%s' not found\n", prog_names[i]))
0034             goto cleanup;
0035     }
0036 
0037     err = bpf_object__load(obj);
0038     if (CHECK(err, "obj_load", "err %d\n", err))
0039         goto cleanup;
0040 
0041     results_map_fd = bpf_find_map(__func__, obj, "test_pro.bss");
0042     if (CHECK(results_map_fd < 0, "find_bss_map",
0043           "err %d\n", results_map_fd))
0044         goto cleanup;
0045 
0046     for (i = 0; i < prog_count; i++) {
0047         kprobe_links[i] = bpf_program__attach(kprobe_progs[i]);
0048         if (!ASSERT_OK_PTR(kprobe_links[i], "attach_kprobe"))
0049             goto cleanup;
0050     }
0051 
0052     memset(&curr, 0, sizeof(curr));
0053     in->sin_family = AF_INET;
0054     in->sin_port = htons(5555);
0055     in->sin_addr.s_addr = inet_addr("255.255.255.255");
0056     memcpy(&orig, &curr, sizeof(curr));
0057 
0058     sock_fd = socket(AF_INET, SOCK_STREAM, 0);
0059     if (CHECK(sock_fd < 0, "create_sock_fd", "err %d\n", sock_fd))
0060         goto cleanup;
0061 
0062     connect(sock_fd, &curr, sizeof(curr));
0063     close(sock_fd);
0064 
0065     err = bpf_map_lookup_elem(results_map_fd, &zero, &tmp);
0066     if (CHECK(err, "get_kprobe_res",
0067           "failed to get kprobe res: %d\n", err))
0068         goto cleanup;
0069 
0070     in = (struct sockaddr_in *)&tmp;
0071     if (CHECK(memcmp(&tmp, &orig, sizeof(orig)), "check_kprobe_res",
0072           "wrong kprobe res from probe read: %s:%u\n",
0073           inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
0074         goto cleanup;
0075 
0076     memset(&tmp, 0xab, sizeof(tmp));
0077 
0078     in = (struct sockaddr_in *)&curr;
0079     if (CHECK(memcmp(&curr, &tmp, sizeof(tmp)), "check_kprobe_res",
0080           "wrong kprobe res from probe write: %s:%u\n",
0081           inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
0082         goto cleanup;
0083 cleanup:
0084     for (i = 0; i < prog_count; i++)
0085         bpf_link__destroy(kprobe_links[i]);
0086     bpf_object__close(obj);
0087 }