Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.
For bugs related to Red Hat Enterprise Linux 5 product line. The current stable release is 5.10. For Red Hat Enterprise Linux 6 and above, please visit Red Hat JIRA https://issues.redhat.com/secure/CreateIssue!default.jspa?pid=12332745 to report new issues.

Bug 503701

Summary: Cannot process scripts beyond an embedded NULL character when running in 'source' mode
Product: Red Hat Enterprise Linux 5 Reporter: Tomas Smetana <tsmetana>
Component: bashAssignee: Roman Rakus <rrakus>
Status: CLOSED ERRATA QA Contact: BaseOS QE <qe-baseos-auto>
Severity: medium Docs Contact:
Priority: medium    
Version: 5.3CC: bnater, mhusnain, rvokal, tsmetana
Target Milestone: rc   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: bash-3.2-27.el5 Doc Type: Bug Fix
Doc Text:
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.
Story Points: ---
Clone Of:
: 1647755 (view as bug list) Environment:
Last Closed: 2011-07-21 10:37:19 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1647755    
Attachments:
Description Flags
Shell script with NULL char at the place of a command
none
Shell script with NULL char inside of a string
none
Proposed patch none

Description Tomas Smetana 2009-06-02 09:54:28 UTC
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.

Comment 1 Tomas Smetana 2009-06-02 09:55:23 UTC
Created attachment 346221 [details]
Shell script with NULL char at the place of a command

Comment 2 Tomas Smetana 2009-06-02 09:56:00 UTC
Created attachment 346222 [details]
Shell script with NULL char inside of a string

Comment 3 Tomas Smetana 2009-06-02 10:00:09 UTC
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.

Comment 4 Roman Rakus 2009-06-02 11:26:33 UTC
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.

Comment 6 RHEL Program Management 2009-11-06 19:12:47 UTC
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 "?".

Comment 8 RHEL Program Management 2010-08-09 19:26:28 UTC
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.

Comment 9 Roman Rakus 2011-02-21 16:38:35 UTC
Fixed in bash-3.2-27.el5

Comment 11 Branislav NĂ¡ter 2011-04-15 12:22:19 UTC
Bugfix was successfully verified on bash-3.2-29.el5 package no supported architectures.

Sourcing and actual run of script gives same output now.

Comment 12 Misha H. Ali 2011-04-20 09:06:08 UTC
    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.

Comment 13 Misha H. Ali 2011-04-21 00:20:23 UTC
    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.

Comment 14 Misha H. Ali 2011-05-04 01:35:38 UTC
    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.

Comment 15 errata-xmlrpc 2011-07-21 10:37:19 UTC
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