From patchwork Sun Feb 22 20:48:19 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 442314 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 1FCAB1400B7 for ; Mon, 23 Feb 2015 07:49:06 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:from:date:message-id:subject:to :content-type; q=dns; s=default; b=ozy9bA4dM1VDJnoHaLeox5Pij3YMX maUS/H/18yDZd7zFbO0qEOcevnF/rAlLp7OJzdkyb0Ei+NZ9prYnVpPqnWZfzsBH BQQ5PcG6lYzollLvqSetPOzUmU9jbp2gFJUVD0y/iZg/LIbNqWk7u7SVTimqjivi lyPrIjiO5GcJ8o= 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:mime-version:from:date:message-id:subject:to :content-type; s=default; bh=g7lDAsjSQCaH5dvFozWtIza0Ijg=; b=i0Q vIszStwYiD+hxTXubHJiSnfSvxx/qAvy613Oj8Y5eETrG/5nt3G+t5Mb8gQ9PNXg kCEmlutUhl3UZiCObwAWvq7cG1nYHfSde6zpYrDY2Iv7kJy4Iwwtjh3oNmQTl+x3 VkRkJa59T0kMnKRG+d1XiDURTsFyc2zpDBF/BUvo= Received: (qmail 25347 invoked by alias); 22 Feb 2015 20:48:53 -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 25270 invoked by uid 89); 22 Feb 2015 20:48:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=1.0 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, KAM_FROM_URIBL_PCCC, RCVD_IN_DNSWL_LOW, SPF_PASS, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mail-ob0-f170.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:from:date:message-id:subject :to:content-type; bh=H2oZcwCt1WbLv0CoSF7N3CmbEyEapPqjMhd/Av4+DBM=; b=P6AGbgBDReZfZVJ3THl2YHgjNwSgJFI61E8/L86wnKjAdnPDjgFdhTg3q8q3POYiid VGRa4SLlGriSH7gDQxugXF7m2A+klztn7I1qoTJZnWFLtAUCQMGXbKHOTPcx5jj83nXq mmTiUdnKW5pnY9F7R/LTwxPh0hbjXcYoVTm7maj2esnPLhwMxUXxxGeJxLvVb0fEeDgz K1CNnem4hcGHci9Kmk/9eScJFbHsr3YYgJrBTQaQq0JG33W3ASip+zTW+XTzx/GlmMFp QNgBMPZj15RMR8rO74wtBRBLHFBX8QkB3bCsokAgzmvHA2AT6la7+pTXBfq2OnoxpgAY /b8A== X-Gm-Message-State: ALoCoQkxbNZvCTwfanrxpPr/RUI907YYP29cvaLISnL+HoMwaPS3DVlCSkcqXlESUCEnPExZiEY5 X-Received: by 10.202.232.65 with SMTP id f62mr5073545oih.116.1424638129417; Sun, 22 Feb 2015 12:48:49 -0800 (PST) MIME-Version: 1.0 From: Paul Pluzhnikov Date: Sun, 22 Feb 2015 12:48:19 -0800 Message-ID: Subject: [patch] Fix BZ #17916 fopen unbounded stack usage for ccs= modes To: GLIBC Devel Greetings, Attached patch fixes BZ #17916 fopen unbounded stack usage for ccs= modes Tested on Linux/x86_64, no failures. 2015-02-22 Paul Pluzhnikov [BZ #17916] * libio/fileops.c (_IO_new_file_fopen): Limit stack use Thanks, diff --git a/NEWS b/NEWS index 28ef45d..660fbe0 100644 --- a/NEWS +++ b/NEWS @@ -10,8 +10,8 @@ Version 2.22 * The following bugs are resolved with this release: 4719, 13064, 14094, 15319, 15467, 15790, 16560, 17269, 17569, 17588, - 17792, 17912, 17932, 17944, 17949, 17964, 17965, 17967, 17969, 17978, - 17987, 17991, 17996, 17998, 17999. + 17792, 17912, 17916, 17932, 17944, 17949, 17964, 17965, 17967, 17969, + 17978, 17987, 17991, 17996, 17998, 17999. * Character encoding and ctype tables were updated to Unicode 7.0.0, using new generator scripts contributed by Pravin Satpute and Mike FABIAN (Red diff --git a/libio/fileops.c b/libio/fileops.c index 297b478..cf61996 100644 --- a/libio/fileops.c +++ b/libio/fileops.c @@ -353,7 +353,14 @@ _IO_new_file_fopen (_IO_FILE *fp, const char *filename, const char *mode, struct gconv_fcts fcts; struct _IO_codecvt *cc; char *endp = __strchrnul (cs + 5, ','); - char ccs[endp - (cs + 5) + 3]; + const size_t ccs_size = endp - (cs + 5) + 3; + const bool alloca_ccs = __libc_use_alloca (ccs_size); + char *ccs; + + if (__glibc_likely (alloca_ccs)) + ccs = alloca (ccs_size); + else + ccs = malloc (ccs_size); *((char *) __mempcpy (ccs, cs + 5, endp - (cs + 5))) = '\0'; strip (ccs, ccs); @@ -365,10 +372,15 @@ _IO_new_file_fopen (_IO_FILE *fp, const char *filename, const char *mode, This means we cannot proceed since the user explicitly asked for these. */ (void) _IO_file_close_it (fp); + if (! __glibc_likely (alloca_ccs)) + free (ccs); __set_errno (EINVAL); return NULL; } + if (! __glibc_likely (alloca_ccs)) + free (ccs); + assert (fcts.towc_nsteps == 1); assert (fcts.tomb_nsteps == 1);