-
Notifications
You must be signed in to change notification settings - Fork 430
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
Add a check if the second argument of logging macros is a string literal #1918
base: rolling
Are you sure you want to change the base?
Add a check if the second argument of logging macros is a string literal #1918
Conversation
Signed-off-by: Artem Shumov <agshumov@sberautotech.ru>
|
Signed-off-by: Artem Shumov <agshumov@sberautotech.ru>
Signed-off-by: Artem Shumov <agshumov@sberautotech.ru>
@aprotyas. Some build failure tests already exist (but disabled). I used a similar way to make the test |
I don't understand, what is the upside to this charge? Is there something wrong with using a not string literal with the macros? |
|
I don't understand how this fixes the ability to use string parsing to execute code as you still allow "%s" but I understand the reasoning at least. Some day it would be nice to migrate away from these macros to something more modern... Maybe something that uses fmt and sbdlog directly as there is only one logging back end anyway. Using fmt (or std::format from 20) would fix the security issue and enable a ton of nice functionality. |
Another option would be to constrain the input to the string_view type and drop all the c-style formatting. std::string and char* are both convertible to string_view and if a user wants to do c-style formatting which has this problem, they can do that on their own. |
I think https://owasp.org/www-community/attacks/Format_string_attack explains it pretty well, but in my understanding the problem is that if you pass a raw string in (like
I would be all for adding options using either |
I think it is helpful to have examples to talk about. How does this stop a user from doing this: https://godbolt.org/z/sKGEPcYr3 You are trying to protect users who log things that they might get from somewhere else. But that doesn't fix the problem of the user forgetting arguments and giving you a "%s" or something similar. |
Gcc like compilers have This PR is just a protection against using:
|
As @shumov-ag said, that will generally give you a compiler warning. Using the code in your link, and compiling with g++ will give you:
|
Thank you for the detailed explanations. This change looks like it does what is intended. I'm sorry if my questions were annoying. |
While I think this is a good change to make, perhaps we consider making this a deprecation warning instead of a compiler error?This would avoid outright breaking downstream code. Thoughts? |
I much prefer a compiler error to a surprise runtime error. |
agree.
i understand this is what we usually do. but since this security issue, probably it could be different? |
The thing is, this should already a warning to users; they'll get the equivalent of what I put in #1918 (comment) (even if they don't have With that in mind, I first rebuilt a local ROS 2 core workspace with this patch in place. Then I went and downloaded all of the packages currently released into Rolling (
(note that there may be more hidden behind these; if any package fails, all things that depend on it are skipped as well so there may be other errors lurking) Before we put this change in, I think we should consider fixing up the failing parts of the ecosystem. That will make it much easier for this to go in without breaking things downstream. I'll at least start with teleop_twist_joy, since that is one that I maintain. |
I'm a bit confused. Currently, I do not see a warning when trying to compile the following: std::string log_message = "Created publisher";
RCLCPP_INFO(this->get_logger(), log_message.c_str()); I'm pretty sure -Wall is set in the CMakeLists.txt too. So, from my perspective the change proposed in this PR goes from no warning to an build error directly. I've pushed the complete example to a branch here: https://github.com/ros2/examples/blob/jacob/repro_rclcpp_1918/rclcpp/topics/minimal_publisher/lambda.cpp Built with:
|
Also, what I am proposing is introducing a build-time warning (if feasible), not necessarily a runtime warning. |
I'm also confused. I know for a fact that in the past, we had warnings on things like:
(we have PRs, like #1442 , where we fixed these and turned on additional warning). But like you, I can't actually make this warning happen currently, and I'm not sure why. Possibly we need to do some additional annotation on the macros to make this happen, and that might actually accomplish what you are asking for; making this a warning first. |
Signed-off-by: Artem Shumov agshumov@sberautotech.ru
This PR prevents non string literal from being passed on to RCLCPP_@(severity) macros