Bug 986095 - bash bug, invalid array index of the_history
bash bug, invalid array index of the_history
Product: Red Hat Enterprise Linux 6
Classification: Red Hat
Component: bash (Show other bugs)
All Linux
medium Severity medium
: rc
: ---
Assigned To: Ondrej Oprala
Jan Kepler
Depends On:
Blocks: 1056252 1070830 1126396
  Show dependency treegraph
Reported: 2013-07-18 20:55 EDT by Geng Sheng Liu
Modified: 2014-10-14 03:09 EDT (History)
4 users (show)

See Also:
Fixed In Version: bash-4.1.2-20.el6
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
: 1126396 (view as bug list)
Last Closed: 2014-10-14 03:09:48 EDT
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Geng Sheng Liu 2013-07-18 20:55:35 EDT
Description of problem:
I've sent the bug description and fix to bash-bug mail list, please refer to link below.


Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:


create a new user like jack.

setup the 4 factors to reproduce the problem, user with the following condition would encount this problem.

1.HISTFILESIZE is larger than 0, for example 1000


3..bash_history is not a empty file

4.time stamp is enabled in .bash_history file.

Then we use root user to run su  - jack, bash would hang.

Actual results:

bash hangs there.

Expected results:

we should switch to user jack successfully.

Additional info:
Comment 1 Geng Sheng Liu 2013-07-18 20:56:39 EDT
I did some investigation and I think it should be a bug of bash.

1. if HISTSIZE=0, then the length of array to store history command in memory would be zero.

The global variable the_history[history_length] is the array to store history command, history_length would be set 0 if HISTSIZE=0,

2. if HISTFILESIZE is set to none zero value, for example 1000, bash would truncate .bash_history size to 1000.

    history_truncate_file (".bash_hisotry, 1000);

3. then read_history_range function would be call to put item read from .bash_history file to array the_history[history_length].

    add_history would have this action done,

add_history (string)
     const char *string;
  HIST_ENTRY *temp;

  if (history_stifled && (history_length == history_max_entries))
      register int i;

      /* If the history is stifled, and history_length is zero,
         and it equals history_max_entries, we don't save items. */
      if (history_length == 0)
return; <--- we can see that if history_length=0, add_history would return directly, would not add any item.

4. Then it would add try to add timestamp if they are enable in .bash_history.

    if (HIST_TIMESTAMP_START(line_start) == 0)
                add_history (line_start);
                if (last_ts)
                    add_history_time (last_ts);
                    last_ts = NULL;

5. Because add_history did not any thing if history_length = 0. and add_history_time would meet a wrong array index exception at

hs = the_history[history_length - 1];

it try to read the_history[-1] which does not exist.

add_history_time (string)
     const char *string;

  hs = the_history[history_length - 1];
  FREE (hs->timestamp);
  hs->timestamp = savestring (string);

6. So the problem happens and shell stops there.

(gdb) where
#0 0x0000000000482027 in add_history_time (string=0x18779b55 "#1357531487") at history.c:322 #1 0x0000000000484d26 in read_history_range (filename=<value optimized out>, from=0, to=4535) at histfile.c:272
#2  0x000000000044de3e in load_history () at bashhist.c:284
#3 0x000000000041b445 in main (argc=<value optimized out>, argv=0x7fff6eee2458, env=0x7fff6eee2468) at shell.c:710
(gdb) f
#0 0x0000000000482027 in add_history_time (string=0x18779b55 "#1357531487") at history.c:322
322      hs = the_history[history_length - 1];


We should exit add_history_time if history_length=0.

/* Change the time stamp of the most recent history entry to STRING. */
add_history_time (string)
     const char *string;

if ( history_length ==0 ) <-----------------------change needs to be done here to avoid invalid array index if history_length=0.

  hs = the_history[history_length - 1];
  FREE (hs->timestamp);
  hs->timestamp = savestring (string);

Gengsheng Liu


Redhat GSS Support
Comment 3 RHEL Product and Program Management 2013-10-13 19:22:08 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 unable to address this
request at this time.

Red Hat invites you to ask your support representative to
propose this request, if appropriate, in the next release of
Red Hat Enterprise Linux.
Comment 8 errata-xmlrpc 2014-10-14 03:09:48 EDT
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.


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