r/C_Programming • • 14d ago

Project My FOSS C project just got its first security advisory. Glad its open source.

Hey guys, so as the title already says, I have just gotten my first security advisory for my C project. I know its not really something to be proud of, or to brag about, but I already expected it, as its (atleast in my opinion) really difficult to have a bullet-proof program in C.

My project github.com/cherries-works/pulse has been in the making for more than 6 months now, and I always thought whether or not it was a smart decision making it open source, or whether or not it might have been smarter to keep it closed source, work on it on my own, release it under a commercial license yadda yadda. However, during my work hours, I checked into the repo real quick, just to find a security advisory laying there. It was not something too bad, it was a badly written parser for HTTP JSON requests on my written HTTP server.

Note that, this issue occurred even though I also added Vibe as a Pentester for my repository (locally). Meaning that (surprise surprise) even AI isnt that good with C.

The issue was that I was snprintf'ing a string with NULL args, and the value of the string that I was parsing included process names (the top ones / sorted by cpu/ram). Now that resulted in a user being able to create a process called "%s%s%s%s%s%s", and that passed into snprintf(...), with NULL args resulted in undefined behavior or more specifically as the publisher mentioned;

"In every run, the six %s specifiers are each consumed by snprintf thereby dereferencing whatever pointer value occupies the next variadic argument slot: (null) for null pointer, raw non-printable bytes for othres."

The problem;

```
snprintf(
response,
response_size,
json,
NULL
);
```

Where json was the char* json value passed into the routeJSON function. The fix was really simple.

```
snprintf(
response,
response_size,
"%s",
json
);
```

Not only did that show me that C still makes it realllyy easy to shoot yourself in the foot, it also made me appreciate open source a lot more. For anyone interested checking out the security advisory; https://github.com/cherries-works/pulse/security/advisories/GHSA-rhqf-rj5j-3v5r, as mentioned it is not really too bad (severity: low), regardless, I am still very glad that my project is open source, and glad that it is in C!

I guess I gotta be more careful with how I handle my stuff.

7 Upvotes

9 comments sorted by

•

u/AutoModerator 14d ago

Hi /u/xerrs_,

Your submission in r/C_Programming was filtered because it links to a git project.

You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.

While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.

You can read more at How do I post my Project?


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

→ More replies (2)

6

u/21Ali-ANinja69 13d ago

Clangd actually warned me when I was did a similar thing (using a variable as the format string for printf). I use -Wall -Wextra and -Wpedantic with clangd

6

u/BlackberryDry4062 13d ago

I see you've rolled your own HTTP server and are hand-crafting JSON. There's several in-process httpd libraries, personally I quite like libmicrohttpd, which is flexible and has plenty of platform support. There's also a json parser/builder for c, called json-c.

While I deeply appreciate rolling your own, if only for the love of the game, those two libraries will save you a ton of work (beyond the initial conversion) when you inevitably add more features :-)

Source: used them both myself extensively in a metrics handling system.

1

u/tiedyerenegade 13d ago

Why are you using snprintf to copy json into response?

1

u/Muffindrake 13d ago

Why are you using snprintf to copy json into response?

Using snprintf as a glorified memcpy is perfectly okay.

Furthermore it is sometimes used to generate boilerplate code, to work faster. No AI code touches the codebase, ever. Every line of code is human written (and understood).

What is not okay is the OP lying in the disclosure about the use of AI, where the boilerplate code uses several different styles all the time.

In places it uses snprintf with a buffer of size BUFFER_1KB, whereas in others it stores that macro/enum in a variable and uses that variable, and others again it uses straight sizeof. There's also a bunch of weird constructs in the codebase ...

1

u/tiedyerenegade 12d ago

Using snprintf as a glorified memcpy is perfectly okay.

It may be "ok" in the sense that it gets the job done. But it's the wrong tool for the job, given it can be misused, exposing the code to vulnerabilities, exactly as OP discovered.

1

u/Maqi-X 13d ago

I don't understand what was the NULL supposed to mean, and why even use sprintf here. Have you considered strncpy?