From patchwork Wed Sep 5 13:38:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Liebler X-Patchwork-Id: 966395 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-95681-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="tH9JJ5vO"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4254Xb0C4sz9sCf for ; Wed, 5 Sep 2018 23:38:22 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:to:cc:from:subject:date:mime-version :content-type:message-id; q=dns; s=default; b=xxbTlle1ecNXIK1d0P ly8JXhay1Hjly+avhnx0XQcwf6y8R5nq+BBmQS6cppk3SKFlppjetjhZpa4h3l8R IoO8/Jmv6Q1sRdzNzduPLNY5WeqeX/6aRA2FidnS77WaCeXHgwsQGc3rSYdM/jBc OsiS8W7rq1pKnIxHF9MpQs7Ug= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:to:cc:from:subject:date:mime-version :content-type:message-id; s=default; bh=8GiAXwNKK5uc4gBYDlhZA7YG gAs=; b=tH9JJ5vOFF0rTFJrYaDtORCqUkTfbPT62KvtBKeEmxnOeDmWOeFschJ5 VF5zO8DzNd4bNLlMhbpVFTchNL/dP/dH0I4XZxAZIXKZj9WlQexZrV+9rKxKXkz6 SGRdCipWR1krDRvfJSQEX6SIMXsMOMdhVOrWbDjrcwzGo43Cvr0= Received: (qmail 77001 invoked by alias); 5 Sep 2018 13:38:17 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 76992 invoked by uid 89); 5 Sep 2018 13:38:17 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=inclusive, termination X-HELO: mx0a-001b2d01.pphosted.com To: GNU C Library Cc: Adhemerval Zanella From: Stefan Liebler Subject: [PATCH] Fix segfault in maybe_script_execute. Date: Wed, 5 Sep 2018 15:38:08 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 x-cbid: 18090513-0020-0000-0000-000002C1A308 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18090513-0021-0000-0000-0000210ED133 Message-Id: <6fab031c-9748-42e4-7137-55bf9ae59545@linux.ibm.com> Hi, If glibc is built with gcc 8 and -march=z900, the testcase posix/tst-spawn4-compat crashes with a segfault. In function maybe_script_execute, the new_argv array is dynamically initialized on stack with (argc + 1) elements. The function wants to add _PATH_BSHELL as the first argument. But if argc == 1, it writes three instead of two elements: new_argv[0] = (char *) _PATH_BSHELL; new_argv[1] = (char *) args->file; new_argv[2] = NULL; The latter write access writes to the same location where the pointer args is stored on stack. Then it segfaults while accessing args: args->exec (new_argv[0], new_argv, args->envp); If argc > 1, the arguments inclusive the NULL termination of new_argv is copied from argv with memcpy. The implementation assumes that argc == 0 will never happen! Okay to commit? Bye Stefan --- ChangeLog: * sysdeps/unix/sysv/linux/spawni.c (maybe_script_execute): Increment size of new_argv by one. commit d099212b378f23fef38ee3f51168bb247d5f719d Author: Stefan Liebler Date: Wed Sep 5 13:34:44 2018 +0200 Fix segfault in maybe_script_execute. If glibc is built with gcc 8 and -march=z900, the testcase posix/tst-spawn4-compat crashes with a segfault. In function maybe_script_execute, the new_argv array is dynamically initialized on stack with (argc + 1) elements. The function wants to add _PATH_BSHELL as the first argument. But if argc == 1, it writes three instead of two elements: new_argv[0] = (char *) _PATH_BSHELL; new_argv[1] = (char *) args->file; new_argv[2] = NULL; The latter write access writes to the same location where the pointer args is stored on stack. Then it segfaults while accessing args: args->exec (new_argv[0], new_argv, args->envp); If argc > 1, the arguments inclusive the NULL termination of new_argv is copied from argv with memcpy. The implementation assumes that argc == 0 will never happen! ChangeLog: * sysdeps/unix/sysv/linux/spawni.c (maybe_script_execute): Increment size of new_argv by one. diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c index cf0213ece5..85239cedbf 100644 --- a/sysdeps/unix/sysv/linux/spawni.c +++ b/sysdeps/unix/sysv/linux/spawni.c @@ -101,7 +101,7 @@ maybe_script_execute (struct posix_spawn_args *args) ptrdiff_t argc = args->argc; /* Construct an argument list for the shell. */ - char *new_argv[argc + 1]; + char *new_argv[argc + 2]; new_argv[0] = (char *) _PATH_BSHELL; new_argv[1] = (char *) args->file; if (argc > 1)