2

I implemented that strchr()

        global  strchr
strchr:
        cmp     byte[rdi], 0
        je      end
        cmp     [rdi], sil
        je      end
        add     rdi, 1
        jmp     strchr
end:    mov     rax, rdi
        ret

When I preload it as .so using,

export LD_PRELOAD=abs/path/to/lib.so

Ubuntu 16.04 crashes. Sometimes it completly crahses, sometimes it displays SIGILL (corrupted data ?).

When I preload it using opensuse 4, it works.

Any idea why ?

NanoPish
  • 1,379
  • 1
  • 19
  • 35
  • How do you build your shared object? You do rebuild it on the different systems (and not only copy one binary to the other)? – Some programmer dude Mar 15 '17 at 13:56
  • @Someprogrammerdude I build it on each system using nasm -f elf64 asm.asm then gcc -shared asm.o -o lib.so (Makefile) – NanoPish Mar 15 '17 at 13:59
  • 1
    Well this function doesn't conform to `strchr` as it doesn't return NULL pointer when the character isn't found. – Michael Petch Mar 15 '17 at 14:01
  • @MichaelPetch Oooooh I'm stupid. Thank you. – NanoPish Mar 15 '17 at 14:04
  • Should also point out that searching for the `nul` (`\0`) character is also valid as the `nul` is considered part of the string. If someone passes 0 as the character then you are suppose to return a pointer to the NUL terminator character. – Michael Petch Mar 15 '17 at 14:08
  • @MichaelPetch Yes, I modified it so it first looks for searched char, then check if it is end of string. – NanoPish Mar 15 '17 at 14:12

1 Answers1

1

Thanks to Michael Petch :

That strchr() doesn't conform to manual for it doesn't return NULL when character is not found.

Fixed strchr() :

global  strchr
strchr:
        cmp     [rdi], sil;first check for character (useful if user searches '\0')
        je      end
        cmp     byte[rdi], 0;then if it is EoS and the character is not in the string, return NULL
        je      eos
        add     rdi, 1
        jmp     strchr
eos:    mov     rax, 0
        ret
end:    mov     rax, rdi
        ret
NanoPish
  • 1,379
  • 1
  • 19
  • 35