Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parser for array out-of-bounds access panic #1

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

ijc
Copy link

@ijc ijc commented Nov 9, 2016

I tripped over a panic which was down to my rxvt-unicode terminfo file (on Debian) having an odd length name string but an even number of booleans, meaning the code expected an alignment byte where none was actually present and then later got confused.

This PR fixes that and also updates the string array parsing code to be more robust against bogus offsets in the input. Also fixes a w/space issues.

Ian Campbell added 3 commits November 9, 2016 17:28
We need to check the number of bytes in names as well as the booleans since
both can cause us to be oddly aligned by the time we are finished with the
booleans.
If an offset in the string section runs off the end of the string table then
the previous code would panic with an array out of bounds issue.

Restructure the code to check for an initial offset which is too large and use
bytes.IndexByte to hunt for the nul and handle the case where one is not found.
@thaJeztah
Copy link

ping @Nvveen ptal 😃

LK4D4 pushed a commit to moby/moby that referenced this pull request Jan 27, 2017
Although our use of ANSI codes here is rather simple it is generally good
practice to use terminfo in order to be portable to different terminal
emulators.

Vendor github.com/Nvveen/Gotty (actually my fork with a fix, see
Nvveen/Gotty#1) and use that to parse the terminfo
files.

Note that "\e]2K" (clear entire line) is not covered by terminfo. We can
achieve the same end by first clearing from begining of line to cursor
(el1="\e]1K") and then clearing from cursor to end of line (el="\e]k").

Test suite has been updated and forces (either directly or by setting $TERM to
something highly unlikely to exist) the use of the non-terminfo fallbacks which
retains the same output behaviour as previously. This is preferable even to
relying on a well-known and relatively static terminfo (like vt102) since even
that in principal might have different terminfo encodings.

In case terminfo is not available at all for $TERM or doesn't expose the
specific capabilities which we use then fall back to the previous manual
escapes, with the exception that we avoid "\e]2K" as discussed above.

Tested with a manual docker pull with rxvt-unicode ($TERM=rxvt-unicode), xterm
($TERM=xterm), mlterm ($TERM=mlterm) and aterm ($TERM=kterm).

Signed-off-by: Ian Campbell <[email protected]>
Ian Campbell added 5 commits April 6, 2017 10:56
- Checking if the file is unreadable is not needed, just try to open and read
  and handle the error.
- Upon success just return the result rather than breaking from the loop and
  falling through, which requires more complex error handling.
- Upon overall failure return the error directly. There is no need to
  fmt.Sprintf the static string argument to errors.New()

This does not yet correctly handle the case where $TERMINFO contains a value.

Signed-off-by: Ian Campbell <[email protected]>
https://linux.die.net/man/5/terminfo says:
> If the environment variable TERMINFO is set, it is interpreted as the
> pathname of a directory containing the compiled description you are working
> on. Only that directory is searched.

I'd have preferred a more authoritative/OS neutral reference (such as
http://pubs.opengroup.org/onlinepubs/7908799/xcurses/terminfo.html) but was
unable to find one.

Signed-off-by: Ian Campbell <[email protected]>
Using filepath switches from `/` to `\` separators on some platforms which
might be unexpected, so defer such a change to another time/discussion.

Signed-off-by: Ian Campbell <[email protected]>
fsouza added a commit to fsouza/go-dockerclient that referenced this pull request Oct 25, 2017
tommy351 added a commit to tommy351/layercake that referenced this pull request Nov 28, 2018
Use a fork version of Gotty to fix the issue

Ref: Nvveen/Gotty#1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants