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

tap_io::handle_read_event(): fix potential buffer overrun #454

Merged
merged 1 commit into from
Feb 7, 2025

Conversation

KanjiMonster
Copy link
Contributor

@KanjiMonster KanjiMonster commented Dec 17, 2024

When accepting a packet, we may copy more than was allocated:

size_t len = sizeof(std::size_t) + 22 + td->mtu;
...
auto *pkt = (packet *)std::malloc(len);

pkt->len = read(fd, pkt->data, len);
                    ^^^^^^^^^^^^^^

pkt->data is at an offset of pkt, but len is the size of the whole struct. So read() may write up to sizeof(pkt->len) over the end of the allocated pkt struct.

Fix this by reducing the maximum read size by size of pkt->len.

Found via valgrind:

valgrind[1714]: ==1714== Thread 3:
valgrind[1714]: ==1714== Syscall param read(buf) points to unaddressable byte(s)
valgrind[1714]: ==1714==    at 0x625E22C: __libc_read (read.c:26)
valgrind[1714]: ==1714==    by 0x625E22C: read (read.c:24)
valgrind[1714]: ==1714==    by 0x278652: read (unistd.h:38)
valgrind[1714]: ==1714==    by 0x278652: basebox::tap_io::handle_read_event(rofl::cthread&, int) (tap_io.cc:110)
valgrind[1714]: ==1714==    by 0x58DF6C6: rofl::cthread::run_loop() (cthread.cpp:745)
valgrind[1714]: ==1714==    by 0x61ECB39: start_thread (pthread_create.c:442)
valgrind[1714]: ==1714==    by 0x626DA83: clone (clone.S:100)
valgrind[1714]: ==1714==  Address 0xa8ae31a is 0 bytes after a block of size 1,530 alloc'd
valgrind[1714]: ==1714==    at 0x484579B: malloc (vg_replace_malloc.c:381)
valgrind[1714]: ==1714==    by 0x278638: basebox::tap_io::handle_read_event(rofl::cthread&, int) (tap_io.cc:103)
valgrind[1714]: ==1714==    by 0x58DF6C6: rofl::cthread::run_loop() (cthread.cpp:745)
valgrind[1714]: ==1714==    by 0x61ECB39: start_thread (pthread_create.c:442)
valgrind[1714]: ==1714==    by 0x626DA83: clone (clone.S:100)

Fixes: 291591d ("l2 aging added")

When accepting a packet, we may copy more than was allocated:

    size_t len = sizeof(std::size_t) + 22 + td->mtu;
    ...
    auto *pkt = (packet *)std::malloc(len);

    pkt->len = read(fd, pkt->data, len);
                        ^^^^^^^^^^^^^^

pkt->data is at an offset of pkt, but len is the size of the whole
struct. So read may write up to sizeof(pkt->len) over the end of the
allocated pkt struct.

Fix this by reducing the maximum read size by size of pkt->len.

Found via valgrind:

    valgrind[1714]: ==1714== Thread 3:
    valgrind[1714]: ==1714== Syscall param read(buf) points to unaddressable byte(s)
    valgrind[1714]: ==1714==    at 0x625E22C: __libc_read (read.c:26)
    valgrind[1714]: ==1714==    by 0x625E22C: read (read.c:24)
    valgrind[1714]: ==1714==    by 0x278652: read (unistd.h:38)
    valgrind[1714]: ==1714==    by 0x278652: basebox::tap_io::handle_read_event(rofl::cthread&, int) (tap_io.cc:110)
    valgrind[1714]: ==1714==    by 0x58DF6C6: rofl::cthread::run_loop() (cthread.cpp:745)
    valgrind[1714]: ==1714==    by 0x61ECB39: start_thread (pthread_create.c:442)
    valgrind[1714]: ==1714==    by 0x626DA83: clone (clone.S:100)
    valgrind[1714]: ==1714==  Address 0xa8ae31a is 0 bytes after a block of size 1,530 alloc'd
    valgrind[1714]: ==1714==    at 0x484579B: malloc (vg_replace_malloc.c:381)
    valgrind[1714]: ==1714==    by 0x278638: basebox::tap_io::handle_read_event(rofl::cthread&, int) (tap_io.cc:103)
    valgrind[1714]: ==1714==    by 0x58DF6C6: rofl::cthread::run_loop() (cthread.cpp:745)
    valgrind[1714]: ==1714==    by 0x61ECB39: start_thread (pthread_create.c:442)
    valgrind[1714]: ==1714==    by 0x626DA83: clone (clone.S:100)

Fixes: 291591d ("l2 aging added")
Signed-off-by: Jonas Gorski <[email protected]>
@KanjiMonster
Copy link
Contributor Author

Not sure how much this is an actual issue.

@jklare jklare merged commit 73f69dc into main Feb 7, 2025
5 checks passed
@jklare jklare deleted the jogo_fix_tap_buffer_overrun branch February 7, 2025 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants