From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id EFCD43834E0A; Mon, 4 Dec 2023 22:06:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EFCD43834E0A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1701727582; bh=9sRrS/tAZT0VMdaaIal9kpq7V4c3dPwLIeVDB++YGSY=; h=From:To:Subject:Date:From; b=NMEubX8iKU18IijLF8htEg0oVYyuE7GSFjGk5khT56lnjgqyrBZalF/Zwj1SzQ4Js 4h/GAxf1fEV+Mn88N+fZZ7xRFGnCPVNP2dqreEijEaD+a7U0P2XTw9IVLdUN755dHN m6qKefXPD3q4z5YNLqe7+p9HXTsRxH4lJyDiydJk= From: "dmalcolm at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug analyzer/112850] New: -Wanalyzer-tainted-allocation-size false positive seen in Linux kernel's sound/core/rawmidi.c Date: Mon, 04 Dec 2023 22:06:22 +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: unknown 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 blocked 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112850 Bug ID: 112850 Summary: -Wanalyzer-tainted-allocation-size false positive seen in Linux kernel's sound/core/rawmidi.c 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: --- False positive at -O1 and above with: /* { dg-do compile } */ /* { dg-options "-fanalyzer -O2" } */ /* { dg-require-effective-target analyzer } */ typedef unsigned long __kernel_ulong_t; typedef __kernel_ulong_t __kernel_size_t; typedef __kernel_size_t size_t; typedef unsigned int gfp_t; extern unsigned long copy_from_user(void* to, const void* from, unsigned lo= ng n); extern __attribute__((__alloc_size__(1))) __attribute__((__malloc__)) void* kvzalloc(size_t size, gfp_t flags); struct snd_rawmidi_params { int stream; size_t buffer_size; }; char *newbuf; static int resize_runtime_buffer(struct snd_rawmidi_params* params) { if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) return -22; newbuf =3D kvzalloc(params->buffer_size, /* { dg-bogus "use of attacker-controlled value '\\*params.buffer_size' as allocation size without upper-bounds checking" } */ (((gfp_t)(0x400u | 0x800u)) | ((gfp_t)0x40u) | ((gfp_t)0x80u))); if (!newbuf) return -12; return 0; } long snd_rawmidi_ioctl(unsigned long arg) { void* argp =3D (void*)arg; struct snd_rawmidi_params params; if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params))) return -14; return resize_runtime_buffer(¶ms); } with the analyzer_kernel_plugin.so: gcc/testsuite/gcc.dg/plugin/taint-sound-core-rawmidi.c:30:12: warning: use = of attacker-controlled value =E2=80=98*params.buffer_size=E2=80=99 as allocati= on size without upper-bounds checking [CWE-789] [-Wanalyzer-tainted-allocation-size] 30 | newbuf =3D kvzalloc(params->buffer_size, /* { dg-bogus "use of attacker-controlled value '\\*params.buffer_size' as allocation size without upper-bounds checking" } */ |=20=20=20=20=20=20=20=20=20=20=20 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~= ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~= ~~~~~~ 31 | (((gfp_t)(0x400u | 0x800u)) | ((gfp_t)0x40u) | ((gfp_t)0x80u))); |=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ =E2=80=98snd_rawmidi_ioctl=E2=80=99: events 1-4 | | 38 | snd_rawmidi_ioctl(unsigned long arg) | | ^~~~~~~~~~~~~~~~~ | | | | | (1) entry to =E2=80=98snd_rawmidi_ioctl=E2=80=99 |...... | 42 | if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params))) | | ~ | | | | | (2) following =E2=80=98false=E2=80=99 branch... | 43 | return -14; | 44 | return resize_runtime_buffer(¶ms); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (3) ...to here | | (4) calling =E2=80=98resize_runtime_buffer=E2=80=99 f= rom =E2=80=98snd_rawmidi_ioctl=E2=80=99 | +--> =E2=80=98resize_runtime_buffer=E2=80=99: events 5-8 | | 26 | resize_runtime_buffer(struct snd_rawmidi_params* params) | | ^~~~~~~~~~~~~~~~~~~~~ | | | | | (5) entry to =E2=80=98resize_runtime_buffer=E2=80=99 | 27 | { | 28 | if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) | | ~ | | | | | (6) following =E2=80=98false=E2=80=99 branch... | 29 | return -22; | 30 | newbuf =3D kvzalloc(params->buffer_size, /* { dg-bogu= s "use of attacker-controlled value '\\*params.buffer_size' as allocation size wit= hout upper-bounds checking" } */ | |=20=20=20=20=20=20=20=20=20=20=20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~= ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~= ~~~~~~ | | | | | (7) ...to here | | (8) use of attacker-controlled value =E2=80=98*params.buffer_size=E2=80=99 as allocation size without upper-boun= ds checking | 31 | (((gfp_t)(0x400u | 0x800u)) | ((gfp_t)0x40u) | ((gfp_t)0x80u))); | |=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Reduced from sound/core/rawmidi.c in Linux kernel Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106358 [Bug 106358] [meta-bug] tracker bug for building the Linux kernel with -fanalyzer=