error: preserve errno in crun_error_release - #2254
infomaniac777 wants to merge 1 commit into
Conversation
|
@eriksjolund PTAL |
|
@giuseppe If I understand your comment correctly, we would also need to fix Lines 164 to 165 in 26ea57f before closing #2153. In other words, we add a safety belt but we also fix users of |
I think it is fine to keep the safety belt, it is not really expensive, so that we don't risk regressions with new callers. @infomaniac777 Please use your real name and email both for the commit author and the |
crun_error_release() and crun_error_write_warning_and_release() free internal error buffers (ptr->msg and ptr). Because free() is not guaranteed by POSIX to preserve errno, calling free() can clobber the system call error code before callers can record it with crun_make_error(). Preserve errno on entry with const int saved_errno and restore it before returning. Add unit tests verifying that crun_error_release() and crun_error_write_warning_and_release() preserve errno. Fixes: containers#2153 Signed-off-by: Deepankar Arya <[email protected]>
3a47de3 to
55f9aa2
Compare
|
Updated the commit with real name and email for the DCO sign-off, and addressed @eriksjolund's review suggestions in the unit tests (checking |
kolyshkin
left a comment
There was a problem hiding this comment.
I would drop the tests entirely as it seems they don't really test anything and are not red without the fix. Also the part that checks crun_error_release (NULL) looks identical to test_crun_error_release_null.
To make the test fail before the fix, we either need to have our own customized free/fprintf (using LD_PRELOAD os something like this), and it's just not worth it.
| #include <time.h> | ||
| #include <sys/time.h> | ||
| #include <stdio.h> | ||
| #include <errno.h> |
There was a problem hiding this comment.
nit: this is not needed as utils.h already includes errno.h.
|
Thanks, my remarks have all been addressed. |
Summary
crun_error_release()andcrun_error_write_warning_and_release()free internal error heap buffers (ptr->msgandptr). POSIX explicitly permitsfree()to modifyerrno. In scenarios wherecrun_error_release(err)is invoked to clean up previous error state right after a failed system call, any modification toerrnoduring deallocation can clobber the error code before callers can record it withcrun_make_error(err, errno, ...).This patch preserves
errnoupon entry usingconst int saved_errno = errno;and restores it before returning.Changes
const int saved_errno = errno;at the start ofcrun_error_release()andcrun_error_write_warning_and_release(), and restoreerrno = saved_errno;before exit.<errno.h>insrc/libcrun/error.c.tests/tests_libcrun_errors.cverifying thatcrun_error_release()andcrun_error_write_warning_and_release()preserveerrno.Fixes: #2153