Red Hat Bugzilla – Bug 503701
Cannot process scripts beyond an embedded NULL character when running in 'source' mode
Last modified: 2018-10-27 09:52:26 EDT
Description of problem: When a shell script has a null character embedded in it, bash seems to execute that script differently when it is invoked directly or sourced by the running shell. Version-Release number of selected component (if applicable): bash-3.2-24 How reproducible: always Steps to Reproduce: 1. run the foo.bash script from the commandline: $ ./foo.bash Before NULL After NULL 2. source the same script: $ . ./foo.bash Before NULL 3. run the foo2.bash script: $ ./foo2.bash Before NULL Helloworld After NULL 4. source the foo2.bash script: Before NULL bash: ./foo2.bash: line 3: unexpected EOF while looking for matching `"' bash: ./foo2.bash: line 4: syntax error: unexpected end of file Actual results: The script is being processed differently in both the cases. Expected results: Consistent output. Additional info: The difference is caused by the way the 'source' builtin handles it: It loads the script into memory as a string and then being parses/executes. This way the '\0' character creates premature end of script in the source mode. In case the script is being processed as a stream (running from a commandline) the '\0' characters are ignored. I have tried to examine the POSIX but it doesn't mention how to handle the '\0' character. The other shells treat the '\0' char also in various ways: ksh considers it to be unacceptable and complains about unexpected character any time it encounters it in a script, whereas zsh treats it as an allowed token and interprets it according to its context. Both of them are however consistent in the behaviour in the execute and source modes.
Created attachment 346221 [details] Shell script with NULL char at the place of a command
Created attachment 346222 [details] Shell script with NULL char inside of a string
Created attachment 346223 [details] Proposed patch This is a bit hackish attempt to unify the behaviour -- the script is being checked in the source mode for a NULL character and if found, it's left out. Please consider this as a suggestion. It seemed to me as a simplest solution though.
Yep. Looks like simple, but I'm not sure, if it is good to left out any character. Better should be to count with length of string and not only with terminating \0. Let's see what upstream think about it.
This request was evaluated by Red Hat Product Management for inclusion, but this component is not scheduled to be updated in the current Red Hat Enterprise Linux release. If you would like this request to be reviewed for the next minor release, ask your support representative to set the next rhel-x.y flag to "?".
This request was evaluated by Red Hat Product Management for inclusion in the current release of Red Hat Enterprise Linux. Because the affected component is not scheduled to be updated in the current release, Red Hat is unfortunately unable to address this request at this time. Red Hat invites you to ask your support representative to propose this request, if appropriate and relevant, in the next release of Red Hat Enterprise Linux.
Fixed in bash-3.2-27.el5
Bugfix was successfully verified on bash-3.2-29.el5 package no supported architectures. Sourcing and actual run of script gives same output now.
Technical note added. If any revisions are required, please edit the "Technical Notes" field accordingly. All revisions will be proofread by the Engineering Content Services team. New Contents: When Bash ran a shell script with a null character embedded in it, bash's source builtin parsed the script incorrectly. This is now fixed and bash's source builtin is able to correctly parse null characters in a shell script.
Technical note updated. If any revisions are required, please edit the "Technical Notes" field accordingly. All revisions will be proofread by the Engineering Content Services team. Diffed Contents: @@ -1 +1 @@ -When Bash ran a shell script with a null character embedded in it, bash's source builtin parsed the script incorrectly. This is now fixed and bash's source builtin is able to correctly parse null characters in a shell script.+When Bash ran a shell script with an embedded null character, bash's source builtin parsed the script incorrectly. This is now fixed and bash's source builtin is able to correctly parse null characters in a shell script.
Technical note updated. If any revisions are required, please edit the "Technical Notes" field accordingly. All revisions will be proofread by the Engineering Content Services team. Diffed Contents: @@ -1 +1 @@ -When Bash ran a shell script with an embedded null character, bash's source builtin parsed the script incorrectly. This is now fixed and bash's source builtin is able to correctly parse null characters in a shell script.+When Bash ran a shell script with an embedded null character, bash's source builtin parsed the script incorrectly. This is fixed and bash's source builtin correctly parses shell script null characters.
An advisory has been issued which should help the problem described in this bug report. This report is therefore being closed with a resolution of ERRATA. For more information on therefore solution and/or where to find the updated files, please follow the link below. You may reopen this bug report if the solution does not work for you. http://rhn.redhat.com/errata/RHSA-2011-1073.html