public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/112792] New: -Wanalyzer-out-of-bounds seen on Linux kernel with certain unions
@ 2023-11-30 21:38 dmalcolm at gcc dot gnu.org
  2023-12-15 20:59 ` [Bug analyzer/112792] -Wanalyzer-out-of-bounds false positives " dmalcolm at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2023-11-30 21:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112792

            Bug ID: 112792
           Summary: -Wanalyzer-out-of-bounds seen on Linux kernel with
                    certain unions
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: dmalcolm at gcc dot gnu.org
            Blocks: 106358
  Target Milestone: ---

VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
typedef unsigned int u32;

union msix_perm {
  struct {
    u32 rsvd2 : 8;
    u32 pasid : 20;
  };
  u32 bits;
} __attribute__((__packed__));

union msix_perm mperm;

void idxd_device_set_perm_entry(u32 pasid) {
  mperm.pasid = pasid;
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

False positive with -fanalyzer:
t.c: In function ‘idxd_device_set_perm_entry’:
t.c:14:15: warning: buffer overflow [CWE-787] [-Wanalyzer-out-of-bounds]
   14 |   mperm.pasid = pasid;
      |   ~~~~~~~~~~~~^~~~~~~
  event 1
    |
    |   11 | union msix_perm mperm;
    |      |                 ^~~~~
    |      |                 |
    |      |                 (1) capacity: 4 bytes
    |
    +--> ‘idxd_device_set_perm_entry’: event 2
           |
           |   14 |   mperm.pasid = pasid;
           |      |   ~~~~~~~~~~~~^~~~~~~
           |      |               |
           |      |               (2) out-of-bounds write at byte 4 but ‘mperm’
ends at byte 4
           |
t.c:14:15: note: write of 1 byte to beyond the end of ‘mperm’

                     ┌─────────────────────────────────────────┐
                     │    write from ‘pasid’ (type: ‘u32’)     │
                     └─────────────────────────────────────────┘
                                          │
                                          │
                                          v
  ┌──────────────────────────────────────────────────────────────────────┐
  │                  ‘mperm’ (type: ‘union msix_perm’)                   │
  └──────────────────────────────────────────────────────────────────────┘
  ├──────────────────────────────────┬───────────────────────────────────┤
                                     │
                            ╭────────┴────────╮
                            │capacity: 4 bytes│
                            ╰─────────────────╯




Affects trunk: https://godbolt.org/z/oWoY7j6eY
Affects 13.2:  https://godbolt.org/z/vzdEbq6E1

(reduced from drivers/dma/idxd/device.c)


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106358
[Bug 106358] [meta-bug] tracker bug for building the Linux kernel with
-fanalyzer

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-05-08 19:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-30 21:38 [Bug analyzer/112792] New: -Wanalyzer-out-of-bounds seen on Linux kernel with certain unions dmalcolm at gcc dot gnu.org
2023-12-15 20:59 ` [Bug analyzer/112792] -Wanalyzer-out-of-bounds false positives " dmalcolm at gcc dot gnu.org
2023-12-16 14:04 ` cvs-commit at gcc dot gnu.org
2023-12-16 21:21 ` cvs-commit at gcc dot gnu.org
2023-12-26 20:39 ` dmalcolm at gcc dot gnu.org
2024-04-14  5:04 ` [Bug analyzer/112792] [13 Regression] " pinskia at gcc dot gnu.org
2024-05-08 19:04 ` [Bug analyzer/112792] " dmalcolm at gcc dot gnu.org

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 read-only IMAP folder(s) and NNTP newsgroup(s).