int main(void) { static const char sample[] = "fchownat_sample"; uid_t uid = geteuid(); uid_t gid = getegid(); if (open(sample, O_RDONLY | O_CREAT, 0400) == -1) perror_msg_and_fail("open"); long rc = syscall(__NR_fchownat, AT_FDCWD, sample, uid, gid, 0); printf("fchownat(AT_FDCWD, \"%s\", %d, %d, 0) = %s\n", sample, uid, gid, sprintrc(rc)); if (unlink(sample)) perror_msg_and_fail("unlink"); rc = syscall(__NR_fchownat, AT_FDCWD, sample, -1, -1L, AT_SYMLINK_NOFOLLOW); printf("fchownat(AT_FDCWD, \"%s\", -1, -1, AT_SYMLINK_NOFOLLOW) = %s\n", sample, sprintrc(rc)); puts("+++ exited with 0 +++"); return 0; }
int main(void) { static const kernel_ulong_t bogus_flags1 = (kernel_ulong_t) 0xfacefeeddeadbeefULL | O_NONBLOCK; static const kernel_ulong_t bogus_flags2 = (kernel_ulong_t) 0x55555550ff96b77bULL & ~all_flags; long rc; rc = syscall(__NR_inotify_init1, bogus_flags1); printf("inotify_init1(IN_NONBLOCK|%s%#x) = %s\n", bogus_flags1 & cloexec_flag ? "IN_CLOEXEC|" : "", (unsigned int) (bogus_flags1 & ~all_flags), sprintrc(rc)); rc = syscall(__NR_inotify_init1, bogus_flags2); printf("inotify_init1(%#x /* IN_??? */) = %s\n", (unsigned int) bogus_flags2, sprintrc(rc)); rc = syscall(__NR_inotify_init1, all_flags); printf("inotify_init1(IN_NONBLOCK%s) = %s\n", all_flags & cloexec_flag ? "|IN_CLOEXEC" : "", sprintrc(rc)); puts("+++ exited with 0 +++"); return 0; }
int main(void) { kernel_ulong_t *const rlimit = tail_alloc(sizeof(*rlimit) * 2); const struct xlat *xlat; for (xlat = resources; xlat->str; ++xlat) { unsigned long res = 0xfacefeed00000000 | xlat->val; long rc = syscall(__NR_setrlimit, res, 0); printf("setrlimit(%s, NULL) = %s\n", xlat->str, sprintrc(rc)); struct rlimit libc_rlim = {}; if (getrlimit((int) res, &libc_rlim)) continue; rlimit[0] = libc_rlim.rlim_cur; rlimit[1] = libc_rlim.rlim_max; rc = syscall(__NR_setrlimit, res, rlimit); const char *errstr = sprintrc(rc); printf("setrlimit(%s, {rlim_cur=%s, rlim_max=%s}) = %s\n", xlat->str, sprint_rlim(rlimit[0]), sprint_rlim(rlimit[1]), errstr); } puts("+++ exited with 0 +++"); return 0; }
int main(void) { kernel_ulong_t len; long rc; len = __NEW_UTS_LEN; rc = syscall(__NR_sethostname, 0, len); printf("sethostname(NULL, %u) = %s\n", (unsigned) len, sprintrc(rc)); if (F8ILL_KULONG_MASK) { len |= F8ILL_KULONG_MASK; rc = syscall(__NR_sethostname, 0, len); printf("sethostname(NULL, %u) = %s\n", (unsigned) len, sprintrc(rc)); } len = __NEW_UTS_LEN + 1; void *const p = tail_alloc(len); rc = syscall(__NR_sethostname, p, len); printf("sethostname(%p, %u) = %s\n", p, (unsigned) len, sprintrc(rc)); puts("+++ exited with 0 +++"); return 0; }
int main(void) { static struct { kernel_ulong_t cmd; const char * cmd_str; } cmd_args[] = { { 0, "???" }, { 4, "???" }, { (kernel_ulong_t) 0xdeafbeefdeadc0deULL, "???" }, { 2, "STOP", }, }; static struct { kernel_ulong_t sig; const char * sig_str; } start_sig_args[] = { { 0, "SIG_0" }, { (kernel_ulong_t) 0xfacefeedac0ffeedULL, NULL }, { ARG_STR(SIGALRM) }, { 33, "SIGRT_1" }, { 63, "SIGRT_31" }, }; unsigned int i; long rc; for (i = 0; i < ARRAY_SIZE(cmd_args); i++) { rc = syscall(__NR_s390_runtime_instr, cmd_args[i].cmd, 0xdead); printf("s390_runtime_instr(%d /* S390_RUNTIME_INSTR_%s */) = " "%s\n", (int) cmd_args[i].cmd, cmd_args[i].cmd_str, sprintrc(rc)); } for (i = 0; i < ARRAY_SIZE(start_sig_args); i++) { long saved_errno; rc = syscall(__NR_s390_runtime_instr, 1, start_sig_args[i].sig); saved_errno = errno; printf("s390_runtime_instr(1 /* S390_RUNTIME_INSTR_START */, "); if (start_sig_args[i].sig_str) printf("%s", start_sig_args[i].sig_str); else printf("%d", (int) start_sig_args[i].sig); errno = saved_errno; printf(") = %s\n", sprintrc(rc)); } puts("+++ exited with 0 +++"); return 0; }
static void test_nlmsg_type(const int fd) { long rc; struct nlmsghdr nlh = { .nlmsg_len = sizeof(nlh), .nlmsg_type = SELNL_MSG_SETENFORCE, .nlmsg_flags = NLM_F_REQUEST, }; rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0); printf("sendto(%d, {len=%u, type=SELNL_MSG_SETENFORCE" ", flags=NLM_F_REQUEST, seq=0, pid=0}" ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc)); } static void test_selnl_msg_unspec(const int fd) { void *const nlh0 = tail_alloc(NLMSG_HDRLEN); TEST_NETLINK_(fd, nlh0, 0xffff, "0xffff /* SELNL_MSG_??? */", NLM_F_REQUEST, "NLM_F_REQUEST", 4, "1234", 4, printf("\"\\x31\\x32\\x33\\x34\"")); }
static long sys_sched_setattr(kernel_ulong_t pid, kernel_ulong_t attr, kernel_ulong_t flags) { long rc = syscall(__NR_sched_setattr, pid, attr, flags); errstr = sprintrc(rc); return rc; }
static void do_lookup_cookie(uint64_t cookie, char *buf, kernel_ulong_t len) { long rc; const char *errstr; # if (LONG_MAX > INT_MAX) \ || (defined __x86_64__ && defined __ILP32__) \ || defined LINUX_MIPSN32 rc = syscall(__NR_lookup_dcookie, cookie, buf, len); # else rc = syscall(__NR_lookup_dcookie, LL_VAL_TO_PAIR(cookie), buf, len); # endif errstr = sprintrc(rc); printf("lookup_dcookie(%" PRIu64 ", ", cookie); /* Here, we trust successful return code */ if ((rc >= 0) && (rc < (long) INT_MAX)) { printf("%.*s, ", (int) rc, buf); } else { if (buf != NULL) printf("%p, ", buf); else printf("NULL, "); } printf("%" PRIu64 ") = %s\n", (uint64_t) len, errstr); }
static void do_send(int fd, char *msg, unsigned int msg_size, struct timespec *tmout, bool cropped) { long rc; long saved_errno; do { rc = syscall(__NR_mq_timedsend, fd, msg, msg_size, 42, tmout); saved_errno = errno; printf("mq_timedsend(%d, ", fd); printstr(MSG_START, msg_size > MSG_MAX_UNCUT ? MSG_MAX_UNCUT : msg_size); if (cropped) printf("..."); errno = saved_errno; printf(", %u, 42, {tv_sec=%lld, tv_nsec=%llu}) = %s\n", msg_size, (long long) tmout->tv_sec, zero_extend_signed_to_ull(tmout->tv_nsec), sprintrc(rc)); errno = saved_errno; if (rc == -1) { if (errno == EINTR) continue; perror_msg_and_skip("mq_timedsend"); } # if DUMPIO_WRITE dumpstr(MSG_START, msg_size); # endif } while (rc); }
int main(void) { long rc; /* check how the first argument is decoded */ ngroups = syscall(SYSCALL_NR, 0, 0); printf("%s(0, NULL) = %ld\n", SYSCALL_NAME, ngroups); if (ngroups < 0) perror_msg_and_fail(SYSCALL_NAME); rc = syscall(SYSCALL_NR, (long) 0xffffffff00000000ULL, 0); printf("%s(0, NULL) = %ld\n", SYSCALL_NAME, rc); rc = syscall(SYSCALL_NR, -1U, 0); printf("%s(%u, NULL) = %s\n", SYSCALL_NAME, -1U, sprintrc(rc)); rc = syscall(SYSCALL_NR, -1L, 0); printf("%s(%u, NULL) = %s\n", SYSCALL_NAME, -1U, sprintrc(rc)); const unsigned int ngroups_max = sysconf(_SC_NGROUPS_MAX); rc = syscall(SYSCALL_NR, ngroups_max, 0); printf("%s(%u, NULL) = %s\n", SYSCALL_NAME, ngroups_max, sprintrc(rc)); rc = syscall(SYSCALL_NR, (long) 0xffffffff00000000ULL | ngroups_max, 0); printf("%s(%u, NULL) = %s\n", SYSCALL_NAME, ngroups_max, sprintrc(rc)); /* check how the second argument is decoded */ GID_TYPE *const g1 = tail_alloc(ngroups ? sizeof(*g1) * ngroups : 1); GID_TYPE *const g2 = tail_alloc(sizeof(*g2) * (ngroups + 1)); void *efault = g2 + ngroups + 1; get_groups(ngroups, g1); get_groups(ngroups + 1, g1); get_groups(ngroups + 1, g2); if (ngroups) { rc = syscall(SYSCALL_NR, ngroups, efault); printf("%s(%u, %p) = %s\n", SYSCALL_NAME, (unsigned) ngroups, efault, sprintrc(rc)); } puts("+++ exited with 0 +++"); return 0; }
static long prctl(kernel_ulong_t arg1, kernel_ulong_t arg2) { static const kernel_ulong_t bogus_arg = (kernel_ulong_t) 0xdeadbeefbadc0dedULL; long rc = syscall(__NR_prctl, arg1, arg2, bogus_arg); errstr = sprintrc(rc); return rc; }
int main(void) { if (F8ILL_KULONG_SUPPORTED) { const void *const p = tail_memdup(".", 2); long rc = syscall(__NR_chdir, p); printf("chdir(\".\") = %s\n", sprintrc(rc)); const kernel_ulong_t ill = f8ill_ptr_to_kulong(p); rc = syscall(__NR_chdir, ill); printf("chdir(%#llx) = %s\n", (unsigned long long) ill, sprintrc(rc)); puts("+++ exited with 0 +++"); return 0; } else { return 77; } }
static long invoke_test_syscall(const unsigned int fd, const unsigned int cmd, void *const p) { const kernel_ulong_t kfd = F8ILL_KULONG_MASK | fd; const kernel_ulong_t op = F8ILL_KULONG_MASK | cmd; long rc = syscall(TEST_SYSCALL_NR, kfd, op, (uintptr_t) p); errstr = sprintrc(rc); return rc; }
static long k_utimensat(const kernel_ulong_t dirfd, const kernel_ulong_t pathname, const kernel_ulong_t times, const kernel_ulong_t flags) { long rc = syscall(__NR_utimensat, dirfd, pathname, times, flags); errstr = sprintrc(rc); return rc; }
static void cleanup(void) { long rc; rc = syscall(__NR_mq_unlink, mq_name); printf("mq_unlink(\"%s\") = %s\n", mq_name, sprintrc(rc)); puts("+++ exited with 0 +++"); }
static long k_remap_file_pages(const kernel_ulong_t addr, const kernel_ulong_t size, const kernel_ulong_t prot, const kernel_ulong_t pgoff, const kernel_ulong_t flags) { const long rc = syscall(__NR_remap_file_pages, addr, size, prot, pgoff, flags); errstr = sprintrc(rc); return rc; }
int main(void) { static const char sample[] = "openat.sample"; long fd = syscall(__NR_openat, -100, sample, O_RDONLY|O_CREAT, 0400); printf("openat(AT_FDCWD, \"%s\", O_RDONLY|O_CREAT, 0400) = %s\n", sample, sprintrc(fd)); if (fd != -1) { close(fd); if (unlink(sample) == -1) perror_msg_and_fail("unlink"); fd = syscall(__NR_openat, -100, sample, O_RDONLY); printf("openat(AT_FDCWD, \"%s\", O_RDONLY) = %s\n", sample, sprintrc(fd)); } puts("+++ exited with 0 +++"); return 0; }
static long sys_io_pgetevents(const kernel_ulong_t ctx_id, const kernel_long_t min_nr, const kernel_long_t nr, const kernel_ulong_t events, const kernel_ulong_t timeout, const kernel_ulong_t usig) { long rc = syscall(__NR_io_pgetevents, ctx_id, min_nr, nr, events, timeout, usig); errstr = sprintrc(rc); return rc; }
int main(void) { print_statfs("/proc/self/status", "PROC_SUPER_MAGIC"); print_statfs(".", NULL); long rc = SYSCALL_INVOKE("", -1, 0, sizeof(STRUCT_STATFS)); const char *errstr = sprintrc(rc); PRINT_SYSCALL_HEADER("", -1, sizeof(STRUCT_STATFS)); printf("NULL) = %s\n", errstr); #ifdef CHECK_ODD_SIZE const unsigned long addr = (unsigned long) 0xfacefeeddeadbeef; rc = SYSCALL_INVOKE("", -1, addr, sizeof(STRUCT_STATFS) + 1); errstr = sprintrc(rc); PRINT_SYSCALL_HEADER("", -1, sizeof(STRUCT_STATFS) + 1); printf("%#lx) = %s\n", addr, errstr); #endif puts("+++ exited with 0 +++"); return 0; }
static void test_syscall(const unsigned long nr) { static const kernel_ulong_t a[] = { (kernel_ulong_t) 0xface0fedbadc0dedULL, (kernel_ulong_t) 0xface1fedbadc1dedULL, (kernel_ulong_t) 0xface2fedbadc2dedULL, (kernel_ulong_t) 0xface3fedbadc3dedULL, (kernel_ulong_t) 0xface4fedbadc4dedULL, (kernel_ulong_t) 0xface5fedbadc5dedULL }; long rc = syscall(nr | SYSCALL_BIT, a[0], a[1], a[2], a[3], a[4], a[5]); #if DEBUG_PRINT fprintf(debug_out, "%s: pid %d invalid syscall %#lx\n", strace_name, getpid(), nr | SYSCALL_BIT); #endif #ifdef LINUX_MIPSO32 printf("syscall(%#lx, %#lx, %#lx, %#lx, %#lx, %#lx, %#lx)" " = %s\n", nr | SYSCALL_BIT, a[0], a[1], a[2], a[3], a[4], a[5], sprintrc(rc)); #else printf("syscall_%#lx(%#llx, %#llx, %#llx, %#llx, %#llx, %#llx)" " = %s\n", nr | SYSCALL_BIT, (unsigned long long) a[0], (unsigned long long) a[1], (unsigned long long) a[2], (unsigned long long) a[3], (unsigned long long) a[4], (unsigned long long) a[5], sprintrc(rc)); #endif }
void do_request_key(const char *type, const char *type_str, const char *desc, const char *desc_str, const char *info, const char *info_str, int32_t keyring, const char *keyring_str) { long rc = syscall(__NR_request_key, type, desc, info, keyring); const char *errstr = sprintrc(rc); printf("request_key("); print_val_str(type, type_str); print_val_str(desc, desc_str); print_val_str(info, info_str); if (keyring_str) printf("%s", keyring_str); else printf("%d", keyring); printf(") = %s\n", errstr); }
int main(void) { const pid_t pid = getpid(); write_pidfile(pid); wait_for_peer_invocation(); static const char dir[] = "attach-p-cmd.test cmd"; int rc = chdir(dir); printf("%-5d chdir(\"%s\") = %s\n" "%-5d +++ exited with 0 +++\n", pid, dir, sprintrc(rc), pid); return 0; }
int main(void) { struct perf_event_attr *attr = tail_alloc(sizeof(*attr)); attr->type = PERF_TYPE_HARDWARE; attr->size = sizeof(*attr); struct { struct perf_event_attr *attr; pid_t pid; int cpu; int group_fd; unsigned long flags; const char *flags_str; } args[] = { { NULL, 0xfacef00d, 0xbadabba7, -1, (unsigned long) 0xFFFFFFFFFFFFFFFFLLU, "PERF_FLAG_FD_NO_GROUP|PERF_FLAG_FD_OUTPUT|" "PERF_FLAG_PID_CGROUP|PERF_FLAG_FD_CLOEXEC|" "0x" LONG_STR_PREFIX "fffffff0" }, { attr + 1, 0, 0, 0, 0, "0" }, { attr, -1, -1, 1, PERF_FLAG_FD_CLOEXEC, "PERF_FLAG_FD_CLOEXEC" }, { attr - 1, -100, 100, 0xface1e55, PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT | PERF_FLAG_PID_CGROUP | PERF_FLAG_FD_CLOEXEC, "PERF_FLAG_FD_NO_GROUP|PERF_FLAG_FD_OUTPUT|" "PERF_FLAG_PID_CGROUP|PERF_FLAG_FD_CLOEXEC" }, }; size_t i; int rc; for (i = 0; i < ARRAY_SIZE(args); i++) { rc = syscall(__NR_perf_event_open, args[i].attr, args[i].pid, args[i].cpu, args[i].group_fd, args[i].flags); printf("perf_event_open(%s, %d, %d, %d, %s) = %s\n", printaddr(args[i].attr), args[i].pid, args[i].cpu, args[i].group_fd, args[i].flags_str, sprintrc(rc)); } puts("+++ exited with 0 +++"); return 0; }
int main(void) { static const char name[] = "strace.test"; static const char c_value[] = "foobar"; const char *const z_value = tail_memdup(c_value, sizeof(c_value)); long rc; rc = fsetxattr(-1, name, z_value, sizeof(c_value), XATTR_REPLACE); printf("fsetxattr(-1, \"%.*s\"..., \"%.*s\"..., %u, XATTR_REPLACE) = %s\n", 4, name, 4, c_value, (unsigned) sizeof(c_value), sprintrc(rc)); puts("+++ exited with 0 +++"); return 0; }
/* * Last argument is optional and is used as follows: * * When type is KCMP_EPOLL_TFD, it signalises whether idx2 is a valid * pointer. */ static void do_kcmp(kernel_ulong_t pid1, kernel_ulong_t pid2, kernel_ulong_t type, const char *type_str, kernel_ulong_t idx1, kernel_ulong_t idx2, ...) { long rc; const char *errstr; rc = syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2); errstr = sprintrc(rc); printf("kcmp(%d, %d, ", (int) pid1, (int) pid2); if (type_str) printf("%s", type_str); else printf("%#x /* KCMP_??? */", (int) type); if (type == KCMP_FILE) { printpidfd(", ", pid1, idx1); printpidfd(", ", pid2, idx2); } else if (type == KCMP_EPOLL_TFD) { va_list ap; int valid_ptr; va_start(ap, idx2); valid_ptr = va_arg(ap, int); va_end(ap); printpidfd(", ", pid1, idx1); printf(", "); if (valid_ptr) { struct kcmp_epoll_slot *slot = (struct kcmp_epoll_slot *) (uintptr_t) idx2; printpidfd("{efd=", pid2, slot->efd); printpidfd(", tfd=", pid2, slot->tfd); printf(", toff=%llu}", (unsigned long long) slot->toff); } else { if (idx2) printf("%#llx", (unsigned long long) idx2); else printf("NULL"); } } else if (type > kcmp_max_type) {
void do_add_key(const char *type, const char *type_str, const char *desc, const char *desc_str, const char *payload, const char *payload_str, size_t plen, int32_t keyring, const char *keyring_str) { long rc = syscall(__NR_add_key, type, desc, payload, plen, keyring); const char *errstr = sprintrc(rc); printf("add_key("); print_val_str(type, type_str); print_val_str(desc, desc_str); print_val_str(payload, payload_str); printf("%zu, ", plen); if (keyring_str) printf("%s", keyring_str); else printf("%d", keyring); printf(") = %s\n", errstr); }
int main(void) { const char *const sigs = SIGUSR2 < SIGCHLD ? "USR2 CHLD" : "CHLD USR2"; const unsigned int size = get_sigset_size(); sigset_t mask; sigemptyset(&mask); sigaddset(&mask, SIGUSR2); sigaddset(&mask, SIGCHLD); int fd = signalfd(-1, &mask, O_CLOEXEC | O_NONBLOCK); printf("signalfd4(-1, [%s], %u, SFD_CLOEXEC|SFD_NONBLOCK) = %s\n", sigs, size, sprintrc(fd)); puts("+++ exited with 0 +++"); return 0; }
static long sys_bpf(kernel_ulong_t cmd, kernel_ulong_t attr, kernel_ulong_t size) { long rc = syscall(__NR_bpf, cmd, attr, size); errstr = sprintrc(rc); #ifdef INJECT_RETVAL if (rc != INJECT_RETVAL) error_msg_and_fail("Got a return value of %ld != %d", rc, INJECT_RETVAL); static char inj_errstr[4096]; snprintf(inj_errstr, sizeof(inj_errstr), "%s (INJECTED)", errstr); errstr = inj_errstr; #endif return rc; }
static void do_recv(int fd, char *msg, unsigned int msg_size, struct timespec *tmout, bool cropped) { long rc; long saved_errno; unsigned prio; do { rc = syscall(__NR_mq_timedreceive, fd, msg, MSG_SIZE, &prio, tmout); saved_errno = errno; printf("mq_timedreceive(%d, ", fd); if (rc >= 0) { printstr(MSG_START, rc > MSG_MAX_UNCUT ? MSG_MAX_UNCUT : rc); if (cropped) printf("..."); } else { printf("%p", msg); } errno = saved_errno; printf(", %u, [42], {tv_sec=%lld, tv_nsec=%llu}) = %s\n", MSG_SIZE, (long long) tmout->tv_sec, zero_extend_signed_to_ull(tmout->tv_nsec), sprintrc(rc)); errno = saved_errno; if (rc == -1) { if (errno == EINTR) continue; perror_msg_and_skip("mq_timedreceive"); } if ((rc >= 0) && ((unsigned long) rc != msg_size)) error_msg_and_skip("mq_timedreceive size mismatch" ": expected %u, got %ld", msg_size, rc); # if DUMPIO_READ dumpstr(MSG_START, rc); # endif } while (rc < 0); }
int main(void) { static const kernel_ulong_t flags[] = { 0, (kernel_ulong_t) 0xbadc0ded00000000ULL, (kernel_ulong_t) 0xffff0000eeee1111ULL, (kernel_ulong_t) 0x123456789abcdef0ULL, }; static const struct { kernel_ulong_t val; const char *str; } rights[] = { { (kernel_ulong_t) 0xbadc0ded00000002ULL, sizeof(kernel_ulong_t) > sizeof(int) ? "PKEY_DISABLE_WRITE|0xbadc0ded00000000" : "PKEY_DISABLE_WRITE" }, { 0xdec0ded, "PKEY_DISABLE_ACCESS|0xdec0dec" }, { 0x3, "PKEY_DISABLE_ACCESS|PKEY_DISABLE_WRITE" }, { ARG_STR(0) }, { 0xbadc0dec, "0xbadc0dec /* PKEY_??? */" }, }; long rc; unsigned int i; unsigned int j; for (i = 0; i < ARRAY_SIZE(flags); i++) { for (j = 0; j < ARRAY_SIZE(rights); j++) { rc = syscall(__NR_pkey_alloc, flags[i], rights[j].val); printf("pkey_alloc(%#llx, %s) = %s\n", (unsigned long long) flags[i], rights[j].str, sprintrc(rc)); } } puts("+++ exited with 0 +++"); return 0; }