From: Sasha Levin <sashal@kernel.org>
To: patches@lists.linux.dev, stable@vger.kernel.org
Cc: Xichao Zhao <zhao.xichao@vivo.com>, Jan Kara <jack@suse.cz>,
Kees Cook <kees@kernel.org>, Sasha Levin <sashal@kernel.org>,
viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org,
clang-built-linux@googlegroups.com
Subject: [PATCH AUTOSEL 6.17-5.4] exec: Fix incorrect type for ret
Date: Thu, 2 Oct 2025 11:30:04 -0400 [thread overview]
Message-ID: <20251002153025.2209281-17-sashal@kernel.org> (raw)
In-Reply-To: <20251002153025.2209281-1-sashal@kernel.org>
From: Xichao Zhao <zhao.xichao@vivo.com>
[ Upstream commit 5e088248375d171b80c643051e77ade6b97bc386 ]
In the setup_arg_pages(), ret is declared as an unsigned long.
The ret might take a negative value. Therefore, its type should
be changed to int.
Signed-off-by: Xichao Zhao <zhao.xichao@vivo.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20250825073609.219855-1-zhao.xichao@vivo.com
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
LLM Generated explanations, may be completely bogus:
## Backport Analysis: exec: Fix incorrect type for ret
**Backport Status: YES**
### Executive Summary
This commit fixes a long-standing type correctness bug in
`setup_arg_pages()` where the `ret` variable was incorrectly declared as
`unsigned long` instead of `int`. While this bug has existed since 2007
(commit b6a2fea39318e4) and has not caused known user-facing issues, it
should be backported to stable trees as a low-risk code correctness
improvement.
### Detailed Analysis
#### 1. Nature of the Bug
In fs/exec.c:602, the `ret` variable in `setup_arg_pages()` was declared
as:
```c
unsigned long ret;
```
However, `ret` is used to store return values from functions that return
`int` with standard Linux error codes:
- `mprotect_fixup()` - returns `int` (0 on success, negative error codes
like -EPERM, -ENOMEM)
- `relocate_vma_down()` - returns `int`
- `expand_stack_locked()` - returns `int`
The function `setup_arg_pages()` itself returns `int`, and all error
paths return negative error codes through `ret`.
#### 2. Why This Bug Hasn't Caused Issues
Despite being present for 18 years, this bug hasn't caused observable
problems due to C's type conversion rules:
1. When a negative `int` (e.g., -ENOMEM = -12) is assigned to `unsigned
long`, it gets sign-extended to a large positive value
(0xFFFFFFFFFFFFFFF4 on 64-bit)
2. When this `unsigned long` is returned as `int`, the truncation
preserves the bit pattern, resulting in the correct negative value
3. All error checks in the function (`if (ret)`) work correctly because
non-zero is still non-zero regardless of signedness
#### 3. Why It Should Still Be Fixed
Despite working "by accident," this is a genuine bug that should be
fixed:
1. **Code correctness**: Error codes should always be stored in signed
types - this is a fundamental Linux kernel convention
2. **Type safety violation**: Storing signed error codes in unsigned
variables violates type safety principles
3. **Compiler warnings**: Modern compilers with stricter type checking
may warn about sign mismatches
4. **Future-proofing**: If code is added that relies on `ret` being
signed (e.g., `if (ret < 0)`), it would break with `unsigned long`
5. **Code clarity**: It's confusing and error-prone to store error codes
in unsigned variables
6. **Static analysis**: Tools like Coverity or Clang static analyzer may
flag this as a potential issue
#### 4. Risk Assessment
The fix carries **minimal to zero risk**:
- **Change scope**: Single line change (fs/exec.c:602)
- **Change complexity**: Trivial type change from `unsigned long` to
`int`
- **Testing**: The code paths are well-exercised by every execve()
system call
- **Review quality**: Reviewed by Jan Kara <jack@suse.cz>, a respected
kernel filesystem developer
- **Regression potential**: Extremely low - the conversion from
`unsigned long` to `int` is safe for all existing error code paths
#### 5. Historical Context
- **Bug introduced**: 2007-07-19 by commit b6a2fea39318e4 (Ollie Wild)
- **Bug duration**: Present in kernel for ~18 years
- **Fix author**: Xichao Zhao <zhao.xichao@vivo.com>
- **Fix date**: 2025-08-25
- **Maintainer**: Kees Cook (security/hardening maintainer)
The original bug was introduced as part of a large refactoring that
added variable-length argument support. The incorrect type likely
slipped through because other variables in the function (stack_shift,
stack_base, etc.) are `unsigned long`, and the type mismatch didn't
cause immediate problems.
#### 6. Backport Justification
This commit meets the stable kernel criteria:
✅ **Fixes a bug**: Yes - type correctness bug
✅ **Small and contained**: Yes - single line change
✅ **No architectural changes**: Yes - simple type fix
✅ **Low regression risk**: Yes - extremely low risk
✅ **Improves code quality**: Yes - follows kernel conventions
✅ **Clear benefit**: Yes - prevents potential future issues
While the commit doesn't have a `Fixes:` or `Cc: stable` tag (suggesting
the author saw it as low priority), it's an ideal stable tree candidate
because:
1. It's completely safe
2. It improves correctness
3. It may prevent issues with stricter compiler checks in the future
4. It aligns with kernel coding standards
### Recommendation
**YES - Backport to all active stable trees**
Priority: Low (code quality fix, not a critical bug)
This is a "good housekeeping" fix that improves code correctness without
any meaningful risk. While not urgent, it should be included in stable
trees to maintain code quality and consistency across kernel versions.
fs/exec.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/exec.c b/fs/exec.c
index e861a4b7ffda9..4a89918b761f6 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -599,7 +599,7 @@ int setup_arg_pages(struct linux_binprm *bprm,
unsigned long stack_top,
int executable_stack)
{
- unsigned long ret;
+ int ret;
unsigned long stack_shift;
struct mm_struct *mm = current->mm;
struct vm_area_struct *vma = bprm->vma;
--
2.51.0
next prev parent reply other threads:[~2025-10-02 15:30 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-02 15:29 [PATCH AUTOSEL 6.17-5.4] hfs: fix KMSAN uninit-value issue in hfs_find_set_zero_bits() Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-6.12] arm64: sysreg: Correct sign definitions for EIESB and DoubleLock Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-5.4] hfs: clear offset and space out of valid records in b-tree node Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-5.4] hfsplus: return EIO when type of hidden directory mismatch in hfsplus_fill_super() Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-6.1] powerpc/32: Remove PAGE_KERNEL_TEXT to fix startup failure Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-5.4] m68k: bitops: Fix find_*_bit() signatures Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17] smb: client: make use of ib_wc_status_msg() and skip IB_WC_WR_FLUSH_ERR logging Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-6.16] arm64: realm: ioremap: Allow mapping memory as encrypted Sasha Levin
2025-10-02 16:43 ` Suzuki K Poulose
2025-10-21 15:38 ` Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-6.12] gfs2: Fix unlikely race in gdlm_put_lock Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-6.1] smb: server: let smb_direct_flush_send_list() invalidate a remote key first Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-5.15] nios2: ensure that memblock.current_limit is set when setting pfn limits Sasha Levin
2025-10-02 15:29 ` [PATCH AUTOSEL 6.17-6.12] s390/mm: Use __GFP_ACCOUNT for user page table allocations Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.16] riscv: mm: Return intended SATP mode for noXlvl options Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.16] gfs2: Fix LM_FLAG_TRY* logic in add_to_queue Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.16] dlm: move to rinfo for all middle conversion cases Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-5.4] hfsplus: fix KMSAN uninit-value issue in hfsplus_delete_cat() Sasha Levin
2025-10-02 15:30 ` Sasha Levin [this message]
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-5.4] hfsplus: fix KMSAN uninit-value issue in __hfsplus_ext_cache_extent() Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.1] lkdtm: fortify: Fix potential NULL dereference on kmalloc failure Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.16] riscv: mm: Use mmu-type from FDT to limit SATP mode Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.6] Unbreak 'make tools/*' for user-space targets Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-5.4] hfs: make proper initalization of struct hfs_find_data Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-5.4] hfsplus: fix slab-out-of-bounds read in hfsplus_strcasecmp() Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.16] riscv: cpufeature: add validation for zfa, zfh and zfhmin Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.12] PCI: Test for bit underflow in pcie_set_readrq() Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.16] s390/pkey: Forward keygenflags to ep11_unwrapkey Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.6] drivers/perf: hisi: Relax the event ID check in the framework Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-5.4] hfs: validate record offset in hfsplus_bmap_alloc Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17] smb: client: limit the range of info->receive_credit_target Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-5.4] dlm: check for defined force value in dlm_lockspace_release Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.12] binfmt_elf: preserve original ELF e_flags for core dumps Sasha Levin
2025-10-02 15:58 ` Kees Cook
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.16] arm64: errata: Apply workarounds for Neoverse-V3AE Sasha Levin
2025-10-02 15:30 ` [PATCH AUTOSEL 6.17-6.16] smb: client: queue post_recv_credits_work also if the peer raises the credit target Sasha Levin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251002153025.2209281-17-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=clang-built-linux@googlegroups.com \
--cc=jack@suse.cz \
--cc=kees@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=patches@lists.linux.dev \
--cc=stable@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=zhao.xichao@vivo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).