Skip to content

fix: remove usage of insecure tmpnam #3149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2024

Conversation

fzipi
Copy link
Contributor

@fzipi fzipi commented May 23, 2024

what

  • remove usage of tmpnam
  • apr has primitives for file handling and unique name creation
  • encapsulate mutex creation in one function acquire_global_lock so we DRY
  • create the temporary file template (GLOBAL_LOCK_TEMPLATE) as a definition outside a function
  • move preprocessor directives into the new function
  • add prototype to modsecurity.h file to enable other functions to create global locks (if needed)

why

  • tmpnam is considered unsafe

drawbacks

  • static memory usage goes up a bit: L_tmpnam is 20 bytes, and APR_MAX_PATH should be ~ 4096.

references

airween
airween previously approved these changes May 23, 2024
@airween airween requested a review from marcstern May 23, 2024 20:19
@marcstern
Copy link

Sounds good overall, just a few remarks:

  1. In case of error, we should log the reason and the path
  2. We should create a "get_lock" function englobing apr_file_mktemp(), apr_file_name_get() & apr_global_mutex_create() instead of duplicating this code
  3. temp_path_template should probably be initialized as a static value outside modsecurity_init() in case we want re-use that "get_lock" function at another place
    @fzipi, can you modify the code accordingly?

@fzipi
Copy link
Contributor Author

fzipi commented May 29, 2024

Sure, makes sense. Will push the changes.

@fzipi fzipi force-pushed the fix-tmpnam branch 2 times, most recently from 117915a to 1d83852 Compare May 29, 2024 13:36
@fzipi fzipi requested a review from airween May 29, 2024 18:05
airween
airween previously approved these changes May 29, 2024
Copy link
Member

@airween airween left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

fzipi added 3 commits May 29, 2024 15:18
Signed-off-by: Felipe Zipitria <[email protected]>
Signed-off-by: Felipe Zipitria <[email protected]>
@marcstern
Copy link

acquire_global_lock() prototype should be added in modsecurity.h in case we want ot reuse it elsewhere (or it should be marked as static for now).

The #ifdef __SET_MUTEX_PERMS should also move to acquire_global_lock().

When you're at it:

#ifdef GLOBAL_COLLECTION_LOCK
    msce->geo_lock = NULL;
#endif

This block can be removed, it's redundant with previous line

Thanks for you responsiveness

Copy link

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@fzipi
Copy link
Contributor Author

fzipi commented May 30, 2024

  • consolidated all the preprocessor flags in the function.
  • Also, I think the
#ifdef GLOBAL_COLLECTION_LOCK
    msce->geo_lock = NULL;
#endif

was a typo, and should be msce->dbm_lock instead (see previous line 188).

@marcstern marcstern merged commit b89c447 into owasp-modsecurity:v2/master May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants