From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28832 invoked by alias); 23 Apr 2012 09:29:34 -0000 Received: (qmail 28813 invoked by uid 22791); 23 Apr 2012 09:29:33 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,TW_CP X-Spam-Check-By: sourceware.org Received: from localhost (HELO sourceware.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 23 Apr 2012 09:29:21 +0000 From: "fredrik.hederstierna@securitas-direct.com" To: gdb-prs@sourceware.org Subject: [Bug cli/14011] New: GDB uses strcpy() with undefined behaviour, causing bug in CLI cd_command(). Date: Mon, 23 Apr 2012 09:29:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gdb X-Bugzilla-Component: cli X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: fredrik.hederstierna@securitas-direct.com X-Bugzilla-Status: NEW X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gdb-prs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-prs-owner@sourceware.org X-SW-Source: 2012-q2/txt/msg00044.txt.bz2 http://sourceware.org/bugzilla/show_bug.cgi?id=14011 Bug #: 14011 Summary: GDB uses strcpy() with undefined behaviour, causing bug in CLI cd_command(). Product: gdb Version: 7.4 Status: NEW Severity: normal Priority: P2 Component: cli AssignedTo: unassigned@sourceware.org ReportedBy: fredrik.hederstierna@securitas-direct.com Classification: Unclassified The C standard states that the behavior of strcpy() is undefined when the source and destination objects overlap. Undefined behavior means it may work sometimes, or it may fail, or it may appear to succeed but manifest failure elsewhere in the program. I got a failure running arm-elf-gdb-4.7.0 (compiled with GCC-4.6.1-9ubuntu3) with arguments arm-elf-gdb --cd=../../build/sniffer2/ sniffer2.elf ... Reading symbols from /home/fredrikh/workspace/buile/sniffer2/sniffer2.elf...done. (gdb) Note that letter 'd' in 'build' is overwritten with letter 'e' in current_path. The path to 'buile' is non-existing causing error. I tracked down to the cd_command() function in CLI that was causing the bug. It seems like the code is doing strcpy() on overlapping regions, to eliminate ".." paths, this causing an undefined behaviour. GDB corrupted the dir-path replacing one letter: The standard solution is to replace strcpy() with memmove(), and I submit a proposed patch that fixed the bug. Index: gdb/cli/cli-cmds.c =================================================================== RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v retrieving revision 1.128 diff -r1.128 cli-cmds.c 420c420 < strcpy (p, p + 2); --- > memmove(p, p + 2, strlen(p + 2) + 1); 439c439 < strcpy (q - 1, p + 3); --- > memmove(q - 1, p + 3, strlen(p + 3) + 1); I fear though that there might be more cases in the sources where strcpy() is used this way. Maybe its a good idea to grep 'strcpy' and check that all uses are safe and non-overlapping. Another idea is to use a custom gdb_strcpy() instead, that we know always copy from left-to-right, where we do define behaviour in the overlapping case. Though is a danger to have dependencies on external C-lib implementation of string functions. Thanks & Best Regards, Fredrik Hederstierna Securitas Direct AB Malmoe Sweden -- Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are on the CC list for the bug.