Skip to content

Conversation

NewbieOrange
Copy link
Contributor

The comments for extract_front_continuous and extract_back_continuous indicate they should return nullptr if data are not enough. However this is not checked in code and result in crashes when deserializing invalid or malicious packets.

@NewbieOrange NewbieOrange changed the title fix iovec not checking size before extracting continuous buffer fix iovector not checking size before extracting continuous buffer Oct 2, 2025
Coldwings

This comment was marked as outdated.

Coldwings

This comment was marked as outdated.

Coldwings

This comment was marked as outdated.

@Coldwings Coldwings dismissed their stale review October 9, 2025 01:38

Need more discussion.

Copy link
Collaborator

@Coldwings Coldwings left a comment

Choose a reason for hiding this comment

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

The iovector_view version of extract_front_continuous/extract_back_continuous will do the check as line 120 implemented.

    // try to extract `bytes` bytes from front elemnt (iov[0])
    // return the pointer if succeeded, or nullptr otherwise
    void* extract_front_continuous(size_t bytes)
    {
        auto& f = front();
        if (empty() || f.iov_len < bytes)
            return nullptr;

        f.iov_len -= bytes;
        auto rst = f.iov_base;
        (char*&)f.iov_base += bytes;
        if (f.iov_len == 0)
            pop_front();
        return rst;
    }

Both checked if iovector_view is empty or first buffer part is not enough. That is enough for extract_xxx_continuous at all, since all iovector size (.sum()) is always greater-equal to the size of first / last iovec, the condition you added seems not be needed.

The case of lack of buffer leading to crash, could you please add a test case or example to show it?

@NewbieOrange
Copy link
Contributor Author

NewbieOrange commented Oct 9, 2025

@Coldwings the case is when the bytes argument is larger than the entire iovec (and the first iovec, obviously), extract_front_continuous() returns nullptr and then with the following code:

when bytes is very large (>>> system memory limit)

auto buf = do_malloc(bytes);
auto ret = extract_front(bytes, buf);
        return ret == bytes ?
            buf : nullptr;

the above code then crash with segfault when bytes is very large.

The bytes argument is passed from the rpc.h/rpc.cc (not user code), parsed from the request body and then passed to extract_front() unchecked. Since the comment mentioned the function should simply return nullptr on bytes > sum(), I suppose the fix should be done in iovec.

@NewbieOrange NewbieOrange requested a review from Coldwings October 9, 2025 02:28
@Coldwings
Copy link
Collaborator

Coldwings commented Oct 9, 2025

@Coldwings the case is when the bytes argument is larger than the entire iovec (and the first iovec, obviously), extract_front_continuous() returns nullptr and then with the following code:

when bytes is very large (>>> system memory limit)

auto buf = do_malloc(bytes);
auto ret = extract_front(bytes, buf);
        return ret == bytes ?
            buf : nullptr;

the above code then crash with segfault when bytes is very large.

The bytes argument is passed from the rpc.h/rpc.cc (not user code), parsed from the request body and then passed to extract_front() unchecked. Since the comment mentioned the function should simply return nullptr on bytes > sum(), I suppose the fix should be done in iovec.

Got it. It seems better to make the condition return just before do_malloc call.

@NewbieOrange
Copy link
Contributor Author

@Coldwings sure, updated

@NewbieOrange NewbieOrange marked this pull request as draft October 9, 2025 03:53
@NewbieOrange NewbieOrange marked this pull request as ready for review October 9, 2025 03:57
Copy link
Collaborator

@Coldwings Coldwings left a comment

Choose a reason for hiding this comment

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

LGTM

@Coldwings Coldwings merged commit 5d28347 into alibaba:release/0.7 Oct 9, 2025
5 checks passed
photonlibos pushed a commit that referenced this pull request Oct 9, 2025
…1010)

* fix iovec not checking size before extracting continuous buffer
@NewbieOrange NewbieOrange deleted the patch-2 branch October 9, 2025 09:19
Coldwings pushed a commit that referenced this pull request Oct 10, 2025
…1010) (#1011)

* fix iovec not checking size before extracting continuous buffer

Co-authored-by: NewbieOrange <[email protected]>
photonlibos added a commit that referenced this pull request Oct 10, 2025
…1010) (#1011)

* fix iovec not checking size before extracting continuous buffer

Co-authored-by: NewbieOrange <[email protected]>
lihuiba pushed a commit that referenced this pull request Oct 10, 2025
…1010) (#1011) (#1013)

* fix iovec not checking size before extracting continuous buffer

Co-authored-by: NewbieOrange <[email protected]>
photonlibos added a commit that referenced this pull request Oct 10, 2025
…1010) (#1011) (#1013)

* fix iovec not checking size before extracting continuous buffer

Co-authored-by: NewbieOrange <[email protected]>
Coldwings pushed a commit that referenced this pull request Oct 10, 2025
…1010) (#1011) (#1013)

* fix iovec not checking size before extracting continuous buffer

Co-authored-by: NewbieOrange <[email protected]>
lihuiba pushed a commit that referenced this pull request Oct 13, 2025
…1010) (#1011) (#1013) (#1014)

* fix iovec not checking size before extracting continuous buffer

Co-authored-by: NewbieOrange <[email protected]>
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