Can't undate running process binary in Solaris?



Hi

I'm trying to get a running process to update its own binary but for
some reason under Solaris it doesn't work , the binary remains
unchanged even though no errors are returned from any function. Does
anyone know why this might happen? My test code is below:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>

char *tag = "TEST";

int main(int argc, char **argv)
{
struct stat fs;
char *mptr;
char *start;
char *end;
int fd;

if ((fd = open(argv[0],O_RDWR)) == -1)
{
perror("open()"); exit(1);
}

if (fstat(fd,&fs) == -1)
{
perror("fstat()"); exit(1);
}

mptr = (char *)mmap(
NULL,fs.st_size,PROT_READ | PROT_WRITE,MAP_PRIVATE,fd,
0);
if (mptr == MAP_FAILED)
{
perror("mmap()"); exit(1);
}

end = mptr + fs.st_size - strlen(tag) - 1;
for(start = mptr;start != end;++start)
{
if (start[0] == 'T' &&
start[1] == 'E' &&
start[2] == 'S' &&
start[3] == 'T')
{
printf("Found tag at address 0x%08X\n",start);
memcpy(start,"DONE",4);
if (munmap(mptr,fs.st_size) == -1)
{
perror("munmap()"); exit(1);
}
if (close(fd) == -1)
{
perror("close()"); exit(1);
}
return 0;
}
}

puts("Tag not found.");
return 0;
}

Thanks for any help

B2003

.



Relevant Pages

  • Re: home grown strtok() function for review
    ... The curpos _after_ searching for the delim string may be ... // pass a string to the mstrtok function and print out results ... look for NULL mptr from mstrtok ... static char *mstrtok (const char *buf, ...
    (comp.lang.c)
  • Re: Multiple indirection mess-up...
    ... Are these "magic" values given in any standard headers? ... mptr is supposed to hold the same value throughout its lifetime. ... ,---- | cdecl> explain char ... | declare sptr as array 5 of pointer to char ...
    (comp.lang.c)
  • Re: ntpd patch
    ... The issue here is that 'tp' is a 'char' type, ... solaris box complained mightily about passing a char to a ctype macro. ... :> (int) cast is completely wrong and dangerous. ...
    (FreeBSD-Security)
  • SSPI Kerberos for delegation
    ... const char *tokenSource, const char *name = NULL, ... DWORD bufsiz = sizeof buf; ... int n = ib.cbBuffer; ... // wserr() displays winsock errors and aborts. ...
    (microsoft.public.dotnet.framework.remoting)
  • [EXPL] ELOG Remote Shell Exploit
    ... char content; ... static int content_length; ... static unsigned char boundary; ... void get_server_version; ...
    (Securiteam)