1

I'm using the following C program as an example ::

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(int argc, char const *argv[]) {
    srand(time(NULL));
    printf("%d\n", rand());
}

Neither strace nor ltrace can detect the srand or rand calls. Why is that?

sumit@HAL9000:~$ gcc random.c -o random
sumit@HAL9000:~$ ./random 
1460823359
sumit@HAL9000:~$ ltrace ./random 
1937594805
+++ exited (status 0) +++
sumit@HAL9000:~/PlaidCTF/golf.so$ strace ./random 
execve("./random", ["./random"], 0x7ffede6c1b60 /* 56 vars */) = 0
brk(NULL)                               = 0x556f5832e000
...
mprotect(0x7f5eaab03000, 4096, PROT_READ) = 0
munmap(0x7f5eaaab2000, 151418)          = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x1), ...}) = 0
brk(NULL)                               = 0x556f5832e000
brk(0x556f5834f000)                     = 0x556f5834f000
write(1, "714715340\n", 10714715340
)             = 10
exit_group(0)                           = ?
+++ exited with 0 +++
Sumit Ghosh
  • 1,033
  • 10
  • 29
  • 1
    What platform are you on? What implementation of standard library are you using? As for `strace`, it shouldn't intercept `rand` nor `srand`, they are not system calls. I can not replicate on my platform - `ltrace` shows calls to glibc. I suspect something is with your `ltrace` or your envinment. – KamilCuk Apr 22 '20 at 14:24
  • 1
    I think it's because strace traces "system calls and signals are events that happen at the user/kernel interface" and srand isn't itself a system call. It *uses* them but it is not one itself. – Jeff Holt Apr 22 '20 at 14:26
  • @JeffHolt yes you're right, `strace` isn't really supposed to catch it. But `ltrace` should, right? – Sumit Ghosh Apr 22 '20 at 14:36
  • @KamilCuk I'm on a standard Ubuntu 19.10 system, didn't really mess with the compilers or runtime environment, I don't think so. But yeah, there might be something going wrong with my environment that I don't know. Your `ltrace` shows calls to `glibc`, I see. Is there any way I can debug it, what's going wrong with my `ltrace`? – Sumit Ghosh Apr 22 '20 at 14:39
  • 1
    See the assembly. I see `callq time,__srandom,rand,_IO_printf` in `objdump -S ./a.out | grep -A20 '
    '`. See what does the executable link with `ldd ./random`. I guess therre is a change the gcc inlines the calls, I wonder, I do not have ubuntu to test.
    – KamilCuk Apr 22 '20 at 14:51
  • @KamilCuk There are calls to `time`, `srand`, `rand` and `printf` in the `objdump`, just like it's supposed to be. Hmm... I'll try this on an Ubuntu VM and see if this problem is with just my system. – Sumit Ghosh Apr 22 '20 at 15:00
  • On my ubuntu machine (linux 4.4.0-174 gcc 5.4.0), I cannot reproduce your problem. I see the ltrace command's output showing a call to time, srand, rand, and printf. Just like I would expect. The output of `nm random.o` shows unresolved references to printf, rand, srand, and time. So looking at the assembly is not required. If they didn't show up in the nm output, then their code is "inlined". – Jeff Holt Apr 22 '20 at 15:01
  • 1
    tl;dr of dupe: compile with `gcc random.c -Wl,-z,lazy -o random`. – Joseph Sible-Reinstate Monica Apr 22 '20 at 15:12

0 Answers0