RT-Thread / RT-Thread/rt-thread

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

Open
#10,299 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 vulnerability in the sys_sendto system call in the Smart version of RT-Thread v5.1.0. This vulnerability stems from insufficient pointer validation in the sockaddr_tolwip function, which is called by sys_sendto. The code only checks if the pointer is NULL but fails to verify whether the pointer points to valid memory. If exploited by a compromised user thread, this issue could lead to severe security consequences, including kernel crashes and potential unauthorized memory access.

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 to parameter pointer. The call graph of this vulnerability is as follows:

  1. rt-thread/components/lwp/lwp_syscall.csys_sendto(int socket, const void *dataptr, size_t size, int flags, const struct musl_sockaddr *to, socklen_t tolen)
  2. rt-thread/components/lwp/lwp_syscall.c void sockaddr_tolwip(const struct musl_sockaddr *std, struct sockaddr *lwip)
sysret_t sys_sendto(int socket, const void *dataptr, size_t size, int flags,
    const struct musl_sockaddr *to, socklen_t tolen)
{
    int flgs = 0;
#ifdef ARCH_MM_MMU
    int ret = -1;
    void *kmem = RT_NULL;
#endif

    flgs = netflags_muslc_2_lwip(flags);
#ifdef ARCH_MM_MMU
    //Omitted code
    if (to)
    {
        struct sockaddr sa;
        sockaddr_tolwip(to, &sa); // Vulnerability function

        ret = sendto(socket, kmem, size, flgs, &sa, tolen);
    }
    else
    {
        ret = sendto(socket, kmem, size, flgs, NULL, tolen);
    }

    //Omitted code
    return (ret < 0 ? GET_ERRNO() : ret);
#endif
}

static void sockaddr_tolwip(const struct musl_sockaddr *std, struct sockaddr *lwip)
{
    if (std && lwip)
    {
        lwip->sa_len = sizeof(*lwip);
        lwip->sa_family = (sa_family_t) std->sa_family; // Vulnerability: Only checks if std is NULL, but doesn't verify if it points to valid memory
        memcpy(lwip->sa_data, std->sa_data, sizeof(lwip->sa_data));
    }
}
Vulnerability Description

The vulnerability exists in the sockaddr_tolwip 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 memory. This oversight can lead to a memory fault when dereferencing an invalid pointer. The issue is particularly critical because:

  1. The to 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 memory before dereferencing it
  4. When accessing std->sa_family and std->sa_data, a memory fault will occur if the pointer points to invalid memory
  5. The memcpy operation could potentially copy data from invalid memory locations
Impact

This vulnerability has severe security implications:

  1. Kernel Crash: The most immediate impact is a potential kernel crash due to invalid memory access, leading to a Denial of Service (DoS) condition.
  2. Privilege Escalation: In certain scenarios, this vulnerability could potentially be exploited to access kernel memory, leading to privilege escalation.
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 and trace sys_sendto into sockaddr_tolwip, focusing on how the user-space to pointer is handled before std->sa_family and std->sa_data are accessed. Determine the project's existing approach to pointer validation, then verify that invalid pointers no longer trigger a kernel fault while valid sendto behavior remains intact.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.