Re: Changing argv[0] under Linux.

From: Miquel van Smoorenburg (miquels@cistron.nl)
Date: Tue Jan 14 2003 - 16:55:02 EST


In article <Pine.LNX.3.95.1030114140811.13496A-100000@chaos.analogic.com>,
Richard B. Johnson <root@chaos.analogic.com> wrote:
>On Tue, 14 Jan 2003, DervishD wrote:
>
>> I'm not sure whether this issue belongs to the kernel or to the
>> libc, but I think that is more on the kernel side, that's why I ask
>> here.
>
>Last time I checked argv[0] was 512 bytes. Many daemons overwrite
>it with no problem.

No cigar. This stuff is all set up by the kernel on the stack;
in order you have

        Top of stack at 0xbfffffff
        environ[0]\0environ[1]\0\0 From high ..
        argv[0]\0argv[1]\0\0
        *environ[];
        *argv[]; .. to low.

The kernel only reserves space as needed. If you have an empty
environment, and you copy 512 bytes over argv[0], you'll end
up with a SEGV.

If you want to modify argv[0] etc, loop over argv[], count howmuch
space there is (strlen(argv[0] + 1 + strlen(argv[1] + 1 ... etc)
and make sure you do NOT write a string longer than that. Also
make sure that you end the string with a double \0

Mike.

-- 
They all laughed when I said I wanted to build a joke-telling machine.
Well, I showed them! Nobody's laughing *now*! -- acesteves@clix.pt

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Wed Jan 15 2003 - 22:00:52 EST