public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/111518] New: relro protection not working in riscv
@ 2023-09-21 12:35 sattdeepan.d at samsung dot com
  2023-09-21 12:53 ` [Bug c/111518] " palmer at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: sattdeepan.d at samsung dot com @ 2023-09-21 12:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111518

            Bug ID: 111518
           Summary: relro protection not working in riscv
           Product: gcc
           Version: 13.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sattdeepan.d at samsung dot com
  Target Milestone: ---

-z,relro and/or -z,now flag not working on riscv arch.


Address of printf overwritten to custom address passed as argument, but it
expected to be readonly when full relro protection is enabled

Test code to reproduce(test_relro.c):
-----------------------------------------------

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


int main(int argc, int *argv[])
{
size_t *p = (size_t *) strtol(argv[1], NULL, 16);

p[0] = 0xdeadbeef;

printf("RELRO: %p\n", p);

return 0;
}
-----------------------------------------------

Steps to reproduce:

1. Turn off ASLR:
  echo 0 > /proc/sys/kernel/randomise_va_space

1. Compile with -z,relro,-z,now flag:
gcc -g -Wl,-z,norelro -O0  -o test_partial test_relro.c

2. Check printf address in GOT:
sattdeepan@sri-9052:~$ objdump -R test_partial | grep printf
0000000000012020 R_RISCV_JUMP_SLOT  printf@GLIBC_2.27

3. Running with gdb:
gdb -q test_partial

4. Get load address of printf function:
<base address of main> - <main offset> + <printf offset in GOT>
0x10586 - 0x10586 +  0x12020 ==> 0x12020

5. Pass load address of main as argument

gdb-peda$ r 0x12020
Starting program: /home/user/test_full_riscv 0x12020
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/riscv64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
Warning: 'set logging off', an alias for the command 'set logging enabled', is
deprecated.
Use 'set logging enabled off'.

Warning: 'set logging on', an alias for the command 'set logging enabled', is
deprecated.
Use 'set logging enabled on'.
0x00000000deadbeee in ?? () ====> address of printf overwritten to custom
address passed as argument, but it expected to be readonly
gdb-peda$

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-09-21 16:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-21 12:35 [Bug c/111518] New: relro protection not working in riscv sattdeepan.d at samsung dot com
2023-09-21 12:53 ` [Bug c/111518] " palmer at gcc dot gnu.org
2023-09-21 13:21 ` schwab@linux-m68k.org
2023-09-21 16:05 ` [Bug target/111518] " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).