From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B6EA938582B0; Thu, 7 Jul 2022 21:11:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B6EA938582B0 From: "dmalcolm at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug analyzer/106229] New: False positives from -Wanalyzer-tainted-array-index with unsigned char index Date: Thu, 07 Jul 2022 21:11:18 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: analyzer X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dmalcolm at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: dmalcolm at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Jul 2022 21:11:18 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106229 Bug ID: 106229 Summary: False positives from -Wanalyzer-tainted-array-index with unsigned char index Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: analyzer Assignee: dmalcolm at gcc dot gnu.org Reporter: dmalcolm at gcc dot gnu.org Target Milestone: --- Consider: struct s_12 { unsigned char idx; char buf[256]; }; char __attribute__((tainted_args)) test_12(struct s_12 s) { return s.buf[s.idx]; } Currently with trunk and gcc 12 this gives: : In function 'test_12': :10:15: warning: use of attacker-controlled value 's.idx' in array lookup without bounds checking [CWE-129] [-Wanalyzer-tainted-array-index] 10 | return s.buf[s.idx]; | ~~~~~^~~~~~~ 'test_12': event 1 | | 8 | test_12(struct s_12 s) | | ^~~~~~~ | | | | | (1) function 'test_12' marked with '__attribute__((tainted_arg= s))' | +--> 'test_12': events 2-3 | | 8 | test_12(struct s_12 s) | | ^~~~~~~ | | | | | (2) entry to 'test_12' | 9 | { | 10 | return s.buf[s.idx]; | | ~~~~~~~~~~~~ | | | | | (3) use of attacker-controlled value 's.i= dx' in array lookup without bounds checking https://godbolt.org/z/ozhWdb78G However, given that s.idx is unsigned char, it must be within the valid ran= ge, and so the warning is unhelpful. See on Linux kernel in drivers/tty/vt/keyboard.c where ioctls use a user-supplied index to access the key_maps array: include/linux/keyboard.h:extern unsigned short *key_maps[MAX_NR_KEYMAPS]; include/uapi/linux/keyboard.h:#define MAX_NR_KEYMAPS 256 but the index is unsigned char, so must be within range.=