Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add protection against out-of-bounds read in ttconv #20629

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

Merged
merged 2 commits into from
Jul 13, 2021

Conversation

sauerburger
Copy link
Member

@sauerburger sauerburger commented Jul 11, 2021

This PR addresses #20628.

PR Summary

The member variable font->numTables indicates the number of tables stored in the directory table. The size of the memory allocated for the directory table is derived from this variable. This PR adds a safe-guard in ttfont_sfnts() to prevent ptr to move beyond the end of the table and thus prevent read-access beyond the memory allocated for font->offset_table.

See #20628

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • [N/A] New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

Copy link
Member

@jkseppan jkseppan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Copy link
Contributor

@aitikgupta aitikgupta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! 🎉

Can we introduce a test (likely the code sample in #20628 (comment)) here?
(that raises another bug, but is there a way we can trigger only this out-of-bounds error?)

Or.. testing ttconv snippets are out of scope of Matplotlib?

@QuLogic QuLogic added this to the v3.4.3 milestone Jul 13, 2021
@QuLogic QuLogic linked an issue Jul 13, 2021 that may be closed by this pull request
@QuLogic
Copy link
Member

QuLogic commented Jul 13, 2021

You should try to use the linking keywords when you know this fixes the issue.

Since I've merged the other PR, please add a test here.

@sauerburger
Copy link
Member Author

You should try to use the linking keywords when you know this fixes the issue.

Sorry, about that. I've updated the other open PRs.

Since I've merged the other PR, please add a test here.

Thanks! I'll rebase the PR.

It would be super difficult to test the initial issue, i.e. a test that would fail on an out-of-bounds read. The table search exits in most cases by chance. At some point, random bytes interpreted as a table name make the algorithm think it is already beyond the sought for entry. To test this manually, I ran valgrind with grep 'pprdrv_tt.cpp:7' (since I couldn't get the Python suppression to work) and made sure that this problematic memory access disappears.

I can, however, add a test, like the example, to make sure that the output is valid (but this test would succeed in ~99% of the cases even without this PR).

@QuLogic
Copy link
Member

QuLogic commented Jul 13, 2021

Yes, but it would at least check that the change from the other PR wouldn't break. We'll have to live with the possibility of a flaky error if this one resurfaces, but that's unlikely anyway.

I was able to replicate this issue in valgrind, so at least that's known to be fixed.

This commit adds to counter to track how many tables are read from the table
directory and stops the search for further tables early if the total number of
tables has been reached.
The new test case checks that Type 42 fonts without the prep table (here stix),
are properly embedded. The test succeeds if the embedded font contains values
for searchRange, entrySelector, rangeShift; and no erroneous out-of-bounds read
occurs while searching for the missing prep table.
@sauerburger sauerburger force-pushed the fix-out-of-bounds-read-ttconv branch from 1073013 to deefcba Compare July 13, 2021 13:06
@jkseppan jkseppan merged commit dba02be into matplotlib:master Jul 13, 2021
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Jul 13, 2021
QuLogic added a commit that referenced this pull request Jul 13, 2021
…629-on-v3.4.x

Backport PR #20629 on branch v3.4.x (Add protection against out-of-bounds read in ttconv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Out-of-bounds read leads to crash or broken TrueType fonts
4 participants