Copilot commented on code in PR #21770:
URL: https://github.com/apache/nuttx/pull/21770#discussion_r4564228416
##########
fs/shm/shmfs.c:
##########
@@ -418,10 +420,11 @@ static int shmfs_mmap(FAR struct file *filep,
{
#ifndef CONFIG_BUILD_KERNEL
/* Without an MMU mapping the object address is returned directly, so
- * a mapping longer than the object would expose adjacent heap memory.
+ * a mapping longer than the page-rounded backing would expose
+ * adjacent heap memory.
*/
- if (entry->length > object->length)
+ if (entry->length > ALIGN_UP(object->length, sysconf(_SC_PAGESIZE)))
Review Comment:
Same issue as in allocation: if `sysconf(_SC_PAGESIZE)` fails and returns
-1, the `ALIGN_UP()` result can become enormous after conversion to `size_t`,
and this check may incorrectly allow overly long mappings (which the comment
explicitly tries to prevent). Please validate the page size result (or use a
compile-time page-size macro consistent with NuttX `mmap()` granularity) before
using it in `ALIGN_UP()`.
##########
fs/shm/shmfs_alloc.c:
##########
@@ -52,7 +53,12 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
*/
size_t hdr_size = sizeof(struct shmfs_object_s);
- size_t alloc_size = length;
+
+ /* mmap() hands out this buffer directly: round it up to a page so that
+ * a page-sized mapping of a smaller object stays inside it.
+ */
+
+ size_t alloc_size = ALIGN_UP(length, sysconf(_SC_PAGESIZE));
Review Comment:
`sysconf(_SC_PAGESIZE)` can legally return -1 on error (or other
non-positive values depending on implementation). Passing that directly to
`ALIGN_UP()` (with implicit conversion to `size_t`) can produce a huge
alignment, causing extreme over-allocation or undefined alignment behavior.
Please fetch the page size once, validate it’s > 0, and only then cast to
`size_t` for `ALIGN_UP()`. Alternatively, prefer a NuttX-provided page-size
constant/macro (e.g., from the mm/map headers) if that’s the actual mapping
granularity you need here.
##########
fs/shm/shmfs_alloc.c:
##########
@@ -82,7 +88,7 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
* memory in user heap
*/
- size_t alloc_size = length;
+ size_t alloc_size = ALIGN_UP(length, sysconf(_SC_PAGESIZE));
Review Comment:
`sysconf(_SC_PAGESIZE)` can legally return -1 on error (or other
non-positive values depending on implementation). Passing that directly to
`ALIGN_UP()` (with implicit conversion to `size_t`) can produce a huge
alignment, causing extreme over-allocation or undefined alignment behavior.
Please fetch the page size once, validate it’s > 0, and only then cast to
`size_t` for `ALIGN_UP()`. Alternatively, prefer a NuttX-provided page-size
constant/macro (e.g., from the mm/map headers) if that’s the actual mapping
granularity you need here.
##########
fs/shm/shmfs_alloc.c:
##########
@@ -52,7 +53,12 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
*/
size_t hdr_size = sizeof(struct shmfs_object_s);
- size_t alloc_size = length;
+
+ /* mmap() hands out this buffer directly: round it up to a page so that
+ * a page-sized mapping of a smaller object stays inside it.
+ */
+
+ size_t alloc_size = ALIGN_UP(length, sysconf(_SC_PAGESIZE));
Review Comment:
`ALIGN_UP(length, pagesize)` can overflow when `length` is near `SIZE_MAX`
(e.g., `length + pagesize - 1` wraps). Even if higher layers usually bound shm
sizes, this is a low-cost hardening spot: add an explicit overflow-safe check
(e.g., reject when `length > SIZE_MAX - (pagesize - 1)`) before aligning. This
prevents wrap-around to a small `alloc_size`, which would undermine the safety
goal of the rounding.
##########
fs/shm/shmfs_alloc.c:
##########
@@ -82,7 +88,7 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
* memory in user heap
*/
- size_t alloc_size = length;
+ size_t alloc_size = ALIGN_UP(length, sysconf(_SC_PAGESIZE));
Review Comment:
`ALIGN_UP(length, pagesize)` can overflow when `length` is near `SIZE_MAX`
(e.g., `length + pagesize - 1` wraps). Even if higher layers usually bound shm
sizes, this is a low-cost hardening spot: add an explicit overflow-safe check
(e.g., reject when `length > SIZE_MAX - (pagesize - 1)`) before aligning. This
prevents wrap-around to a small `alloc_size`, which would undermine the safety
goal of the rounding.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]