Skip to content

httpc: fix signed integer overflow #8464

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

Conversation

Totktonada
Copy link
Member

@Totktonada Totktonada commented Mar 18, 2023

The lua_add_key_u64() function pushes an uint64_t value using lua_pushinteger(), which accepts int64_t argument. A value >= 2^63 will be interpreted as a negative value on all architectures we're supporting. However, technically it is implementation defined behavior (see n1256, 6.3.1.3.3).

It is not a problem, in fact, because the function is used only to report http_client:stat() statistics and because values beyond 2^63-1 are unreachable in practice.

OTOH, it is easy to eliminate the undefined behavior by replacing lua_pushinteger() with our own helper function, which accepts uint64_t: luaL_pushuint64().

The values above 10^14 - 1 are now pushed as cdata<uint64_t>. Lower values are pushed as number just like before the commit.

Reported-in: https://github.com/tarantool/security/issues/103

@github-advanced-security
Copy link

You have successfully added a new CodeQL configuration /language:cpp. As part of the setup process, we have scanned this repository and found 21 existing alerts. Please check the repository Security tab to see all alerts.

@coveralls
Copy link

coveralls commented Mar 18, 2023

Coverage Status

Coverage: 85.586% (-0.01%) from 85.596% when pulling 312ad3a on Totktonada:httpc-fix-signed-integer-overflow into 67578d1
on tarantool:master
.

Totktonada added a commit to tarantool/smtp that referenced this pull request Mar 18, 2023
A cast of the value above 2^63-1 to `int64_t` has implementation defined
behavior, see n1256, 6.3.1.3.3. `lua_pushinteger()` accepts `int64_t`,
so passing `uint64_t` value may lead to pushing negative (or other
incorrect) value.

Let's use Tarantool specific `luaL_pushuint64()` function instead.

This is mostly just to make things in the right way: it is unlikely to
see such large values in the statistics in practice.

See all the details in [1].

[1]: tarantool/tarantool#8464
Copy link
Collaborator

@Buristan Buristan left a comment

Choose a reason for hiding this comment

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

Hi, Alexander!
Thanks for the patch!
LGTM, except a single nit regarding the commit message.

will be interpreted as a negative value on all architectures we're
support.

Typo: s/we're support/we're supporting/

@Buristan Buristan assigned Totktonada and unassigned Buristan Mar 18, 2023
The `lua_add_key_u64()` function pushes an `uint64_t` value using
`lua_pushinteger()`, which accepts `int64_t` argument. A value >= 2^63
will be interpreted as a negative value on all architectures we're
supporting. However, technically it is implementation defined behavior
(see n1256, 6.3.1.3.3).

It is not a problem, in fact, because the function is used only to
report `http_client:stat()` statistics and because values beyond 2^63-1
are unreachable in practice.

OTOH, it is easy to eliminate the undefined behavior by replacing
`lua_pushinteger()` with our own helper function, which accepts
`uint64_t`: `luaL_pushuint64()`.

The values above 10^14 - 1 are now pushed as `cdata<uint64_t>`. Lower
values are pushed as `number` just like before the commit.

Reported-in: tarantool/security#103

NO_DOC=The type of values in the statistics is not specified explicitly
       in the documentation (not obligated to be `number`) and it is
       quite common for Tarantool to return a value of `cdata<int64_t>`
       or `cdata<uint64_t>` type for an integer with a large absolute
       value.
NO_CHANGELOG=see NO_DOC
NO_TEST=It is hard to reach so large values externally (send 2^63
        requests) and it doesn't look worthful to introduce an error
        injection/a internal API to test it. `luaL_pushuint64()` is
        covered by the module API test.
@Totktonada Totktonada force-pushed the httpc-fix-signed-integer-overflow branch from 5d57ad0 to 312ad3a Compare March 19, 2023 16:34
@Totktonada
Copy link
Member Author

Typo: s/we're support/we're supporting/

Fixed the typo.

@Totktonada Totktonada added the full-ci Enables all tests for a pull request label Mar 20, 2023
Totktonada added a commit to tarantool/smtp that referenced this pull request Mar 20, 2023
A cast of the value above 2^63-1 to `int64_t` has implementation defined
behavior, see n1256, 6.3.1.3.3. `lua_pushinteger()` accepts `int64_t`,
so passing `uint64_t` value may lead to pushing negative (or other
incorrect) value.

Let's use Tarantool specific `luaL_pushuint64()` function instead.

This is mostly just to make things in the right way: it is unlikely to
see such large values in the statistics in practice.

See all the details in [1].

[1]: tarantool/tarantool#8464
@Totktonada Totktonada merged commit 3dbbf2d into tarantool:master Mar 20, 2023
@Totktonada Totktonada deleted the httpc-fix-signed-integer-overflow branch March 20, 2023 16:53
@Totktonada Totktonada removed their assignment Mar 20, 2023
@Totktonada
Copy link
Member Author

Pushed to master as 3.0.0-entrypoint-80-g3dbbf2d39, cherry-picked to 2.11 as 2.11.0-rc1-67-g430605ef7 and to 2.10 as 2.10.6-entrypoint-22-g5349ec536.

Not pushed to release/2.11.0, so this change will not appear in the upcoming 2.11.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full-ci Enables all tests for a pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants