This service will be undergoing maintenance at 00:00 UTC, 2017-10-23 It is expected to last about 30 minutes
Bug 503701 - Cannot process scripts beyond an embedded NULL character when running in 'source' mode
Cannot process scripts beyond an embedded NULL character when running in 'sou...
Status: CLOSED ERRATA
Product: Red Hat Enterprise Linux 5
Classification: Red Hat
Component: bash (Show other bugs)
5.3
All Linux
medium Severity medium
: rc
: ---
Assigned To: Roman Rakus
BaseOS QE
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2009-06-02 05:54 EDT by Tomas Smetana
Modified: 2014-01-12 19:09 EST (History)
4 users (show)

See Also:
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:
Environment:
Last Closed: 2011-07-21 06:37:19 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)
Shell script with NULL char at the place of a command (55 bytes, text/plain)
2009-06-02 05:55 EDT, Tomas Smetana
no flags Details
Shell script with NULL char inside of a string (69 bytes, text/plain)
2009-06-02 05:56 EDT, Tomas Smetana
no flags Details
Proposed patch (915 bytes, patch)
2009-06-02 06:00 EDT, Tomas Smetana
no flags Details | Diff

  None (edit)
Description Tomas Smetana 2009-06-02 05:54:28 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.
Comment 1 Tomas Smetana 2009-06-02 05:55:23 EDT
Created attachment 346221 [details]
Shell script with NULL char at the place of a command
Comment 2 Tomas Smetana 2009-06-02 05:56:00 EDT
Created attachment 346222 [details]
Shell script with NULL char inside of a string
Comment 3 Tomas Smetana 2009-06-02 06:00:09 EDT
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 07:26:33 EDT
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 Product and Program Management 2009-11-06 14:12:47 EST
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 Product and Program Management 2010-08-09 15:26:28 EDT
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 11:38:35 EST
Fixed in bash-3.2-27.el5
Comment 11 Branislav Náter 2011-04-15 08:22:19 EDT
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 05:06:08 EDT
    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-20 20:20:23 EDT
    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-03 21:35:38 EDT
    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 06:37:19 EDT
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

Note You need to log in before you can comment on or make changes to this bug.