Fix use of strdup on a NULL pointer #63
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
EDIT: The pull request message has been updated with a correction for using
strdup
on a null pointer.The following set of commands can cause a memory fault when auditing is enabled, although it can also cause ksh to write '(null)' to the auditing file in place of a tty name:
The crash can also occur in this regression test, although intermittently:
ksh/src/cmd/ksh93/tests/builtins.sh
Lines 651 to 653 in 300cd19
This happens because
strdup
is used unconditionally on the pointer returned byttyname
, which can be NULL ifstderr
is closed. The string is then set to NULL, which causes the crash as ksh expects a valid pointer to be returned:ksh/src/cmd/ksh93/edit/history.c
Lines 397 to 399 in 300cd19
ksh/src/lib/libast/string/strdup.c
Lines 53 to 60 in 300cd19
From https://pubs.opengroup.org/onlinepubs/9699919799/functions/ttyname.html#tag_16_628_04:
This bug was originally reported in att#1028. The fix (from att#1062) is to have strdup duplicate 'notty' if
ttyname
returns a null pointer, which results in the auditing file now recording 'notty' instead of '(null)':