RT-Thread / RT-Thread/rt-thread

[Bug] Insufficient parameter validation vulnerability in the sys_recvfrom in Smart version of RT-Thread

Open
#10,304 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
12.2k
Forks
5.4k
Avg merge
4d 12h
Merged PRs (30d)
40

Description

RT-Thread Version

v5.1.0

Hardware Type/Architectures

Not Apply

Develop Toolchain

Other

Describe the bug
Summary

I have identified a critical privilege escalation vulnerability in the sys_recvfrom system call in RT-Thread v5.1.0. This vulnerability stems from insufficient validation of the from parameter pointer in the sockaddr_tomusl function. An attacker can control this pointer to write to arbitrary kernel memory locations, potentially leading to privilege escalation and system compromise.

Vulnerable Code Location

The vulnerability is present in the rt-thread/components/lwp/lwp_syscall.c file. The issue arises from the lack of proper validation of the from parameter pointer before writing to it. The call graph of this vulnerability is as follows:

rt-thread/components/lwp/lwp_syscall.c: sys_recvfrom(int socket, void *mem, size_t len, int flags, struct musl_sockaddr *from, socklen_t *fromlen)

rt-thread/components/lwp/lwp_syscall.c: static void sockaddr_tomusl(const struct sockaddr *lwip, struct musl_sockaddr *std)

sysret_t sys_recvfrom(int socket, void *mem, size_t len, int flags,
      struct musl_sockaddr *from, socklen_t *fromlen)
{

   // Omitted code
    if (from)
    {
        struct sockaddr sa;

        ret = recvfrom(socket, kmem, len, flgs, &sa, fromlen);
        sockaddr_tomusl(&sa, from);
    }
    else
    {
        ret = recvfrom(socket, kmem, len, flgs, NULL, NULL);
    }
    // Omitted code
}

static void sockaddr_tomusl(const struct sockaddr *lwip, struct musl_sockaddr *std)
    {
        if (std && lwip)
        {
            std->sa_family = (uint16_t) lwip->sa_family;
            memcpy(std->sa_data, lwip->sa_data, sizeof(std->sa_data)); //Unauthorized memory write possible if std points to kernel memory or sensitive memory.
        }
    }
Vulnerability Description

The vulnerability exists in the sockaddr_tomusl function's handling of the std parameter. The code only performs a NULL check on the std pointer but fails to verify whether the pointer points to valid user-space memory. This oversight allows an attacker to write to arbitrary memory locations, including kernel memory. The issue is particularly critical because:

  1. The from parameter is passed directly from user space to kernel space

  2. The code only checks if the pointer is NULL (if (std && lwip))

  3. No validation is performed to ensure the pointer points to valid user-space memory

  4. The memcpy operation can write to any memory location specified by the attacker

  5. An attacker can control the from pointer to write to:

    • Kernel memory

    • Other processes' memory

    • System-critical memory regions

Impact
  1. This vulnerability has severe security implications:

    Kernel Memory Write: The most critical impact is the ability to write to kernel memory, which could lead to:

    • System crash

    • Data corruption

Other additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in rt-thread/components/lwp/lwp_syscall.c by tracing sys_recvfrom into sockaddr_tomusl and reviewing how the from pointer crosses from user space. Determine the project’s existing pointer-validation patterns and apply them to this path. Done means invalid or unauthorized from pointers cannot cause the conversion to write to kernel or sensitive memory, with the behavior covered by an appropriate test if the repository provides one.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot, operating-systems, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.