-2

In my program I am using secure_getenv function to fetch some environment variables. I included stdlib.h in my program. This is the sample call to secure_getenv.

Line 1 : char *myenv;

Line 2 : myenv = __secure_getenv("DATA");

After the above lines execution, myenv points to some junk.

I tried this within gdb after line 2.

p __secure_getenv("DATA")

This prints me the DATA I wanted.

But when I try, "p myenv", It prints the below.

$2 = 0×fffffffffffffe13f<Address 0xfffffffffffffe13f out of bounds>"

Can the experts help me to understand what is missing & how to make this work.

Edited to add: How the myenv is actually used? In somepoint in time my code tries to call the below.

strlen(myenv);

On strlen function call, my code terminates with sig11(SIGSEGV)

6
  • 1
    If you compiled with optimization, the generated code execution sequence does often not correspond to the source code order, so for example a source line has to be visited multiple times in the debugger before it is completely executed. Try to continue to a line where myenv is actually used, and print it there. Commented Jan 23, 2023 at 13:55
  • Thank you. Just edited my post to add how the myenv is actually used. Commented Jan 23, 2023 at 14:49
  • The code presented does not use secure_getenv(). It uses __secure_getenv(), which I don't think is a documented interface or one intended to be accessed by user code. Commented Jan 23, 2023 at 14:58
  • Please present a minimal reproducible example demonstrating the issue. Include the command you're using to compile the program. Also, pay attention to compiler diagnostics. Unless you're defining __secure_getenv yourself (or not using it after all), or unless you are actively suppressing compiler messages, it is highly unlikely that the compiler is accepting your code without emitting any diagnostics. Commented Jan 23, 2023 at 15:25
  • 1
    Again: minimal reproducible example. And compiler diagnostics. Commented Jan 23, 2023 at 15:30

1 Answer 1

1

Can the experts help me to understand what is missing & how to make this work.

The most likely cause is that you don't have a prototype for __secure_getenv, which means that the compiler assumes that this function returns an int. That int is then cast to char*, which sign-extends it to produce "garbage" pointer 0xfffffffffffffe13f.

  1. You should compile your source with -Wall -Wextra -- the compiler will then warn you about the bug.
  2. You should #include <stdlib.h> and use secure_getenv() instead of __secure_getenv() -- the former will have a proper prototype.
  3. You can compare the output from p __secure_getenv("DATA") -- it will print the data you expect, and also the pointer value. If my guess is correct, the pointer value will have different high-order bits, but same low 32-bits as myenv == 0xfffffffffffffe13f
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.