Bug 395266 - Environment variables with slashes get dropped
Summary: Environment variables with slashes get dropped
Status: RESOLVED NOT A BUG
Alias: None
Product: valgrind
Classification: Developer tools
Component: general (show other bugs)
Version: 3.13.0
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Julian Seward
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-06-12 08:13 UTC by Timo Sirainen
Modified: 2023-01-05 09:16 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Timo Sirainen 2018-06-12 08:13:32 UTC
Example program:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	printf("%s\n", getenv("FOO/BAR"));
	return 0;
}

I can successfully run it with:

env 'FOO/BAR=1' ./test

However, with valgrind FOO/BAR is NULL and it crashes:

env 'FOO/BAR=1' valgrind ./test
==77478== Invalid read of size 1
==77478==    at 0x4C2FD72: strlen (vg_replace_strmem.c:458)
==77478==    by 0x4EA7D01: puts (ioputs.c:35)
==77478==    by 0x1086A1: main (in /home/tss/test)
Comment 1 Paul Floyd 2020-12-09 15:52:22 UTC
I just tried to reproduce this (admittedly on FreeBSD) and could see no problem.

Which shell is this? Not that that should matter.

Can you build Valgrind from source? If so, could you change the variable 'debug' to be 'True' at the start of setup_client_env, and rerun the test.
Comment 2 Timo Sirainen 2020-12-09 16:23:50 UTC
Looks like there's something weird in the Debian packaging. If I get the sources with "apt source valgrind" and then compile it with:

./configure --prefix=/tmp/valgrind && make && sudo make install

Then it works correctly. If I compile the sources with "dpkg-buildpacakge -b" then it's again wrongly NULL. I guess I'll report it to Debian bugs then.
Comment 3 Paul Floyd 2020-12-09 16:55:58 UTC
An optimization (LTO?) problem perhaps. Ill leave this open - can you please update if the Debian folks have more info?
Comment 4 Mark Wielaard 2021-02-28 23:48:40 UTC
I can also not replicate this with the Fedora packaged valgrind.
Comment 5 Paul Floyd 2022-12-26 10:39:37 UTC
There are two possible places that this could be going wrong.

1. The valgrind launcher. This modifies the environment to insert a couple of variables so that Valgrind will be able to load a couple of shared libraries.
2. The tool code that sets up the client env. This synthesizes a new env for the client, adding a LD_PRELOAD.

To eliminate number 1 could you try something like this:

export VALGRIND_LAUNCHER=`which valgrind`
export VALGRIND_LIB=/usr/libexec/valgrind
env 'FOO/BAR=1' /usr/libexec/valgrind/memcheck-amd64-linux  ./395266

(note that this is NOT a recommended way to run Valgrind!)
Comment 6 Timo Sirainen 2023-01-02 01:02:58 UTC
(In reply to Paul Floyd from comment #5)
> To eliminate number 1 could you try something like this:
> 
> export VALGRIND_LAUNCHER=`which valgrind`
> export VALGRIND_LIB=/usr/libexec/valgrind
> env 'FOO/BAR=1' /usr/libexec/valgrind/memcheck-amd64-linux  ./395266

Testing in Ubuntu 20.04 in Macbook it runs correctly (no crash):

export VALGRIND_LAUNCHER=`which valgrind`
export VALGRIND_LIB=/usr/lib/aarch64-linux-gnu/valgrind
env 'FOO/BAR=1' /usr/lib/aarch64-linux-gnu/valgrind/memcheck-arm64-linux ./test
Comment 7 Paul Floyd 2023-01-04 07:17:21 UTC
Do you have the original problem with Ubuntu as well?
Comment 8 Timo Sirainen 2023-01-04 08:31:39 UTC
Yes, original problem exists in Ubuntu 20.04.
Comment 9 Paul Floyd 2023-01-05 09:16:21 UTC
I now see what is happening.

On kubuntu, the valgrind package uses a shell wrapper. This sets a couple of environment variables (LD_LIBRARY_PATH to pick up debug libs and GLIBCXX_FORCE_NEW=1 to prevent new from using a cache). Then it execs valgrind.bin.

Since FOO/BAR is not a valid environment variable (at least , not as far as bash is concerned, env does allow it) then it doesn't get propagated to valgrind.bin and the guest exe crashes.

If you run

env 'FOO/BAR=1' valgrind.bin ./test

then it should work normally.