From patchwork Wed Sep 17 23:48:39 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Todd Vierling X-Patchwork-Id: 4927621 Return-Path: X-Original-To: patchwork-linux-nfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id EF017BEEA5 for ; Wed, 17 Sep 2014 23:48:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2FD5320145 for ; Wed, 17 Sep 2014 23:48:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2919120138 for ; Wed, 17 Sep 2014 23:48:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752207AbaIQXsp (ORCPT ); Wed, 17 Sep 2014 19:48:45 -0400 Received: from userp1040.oracle.com ([156.151.31.81]:37541 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751750AbaIQXso (ORCPT ); Wed, 17 Sep 2014 19:48:44 -0400 Received: from ucsinet22.oracle.com (ucsinet22.oracle.com [156.151.31.94]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id s8HNmfTb010756 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 17 Sep 2014 23:48:42 GMT Received: from ca-server1.us.oracle.com (ca-server1.us.oracle.com [139.185.48.5]) by ucsinet22.oracle.com (8.14.5+Sun/8.14.5) with ESMTP id s8HNmfC5019861; Wed, 17 Sep 2014 23:48:41 GMT Received: from tvierlin by ca-server1.us.oracle.com with local (Exim 4.69) (envelope-from ) id 1XUOxk-0004fu-Rm; Wed, 17 Sep 2014 16:48:40 -0700 From: Todd Vierling To: linux-nfs@vger.kernel.org Subject: [PATCH] exportfs: Properly parse IPv6 literal strings with null termination Date: Wed, 17 Sep 2014 19:48:39 -0400 Message-Id: <1410997719-17913-1-git-send-email-todd.vierling@oracle.com> X-Mailer: git-send-email 1.7.5.1 X-Source-IP: ucsinet22.oracle.com [156.151.31.94] Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The original implementation was using strncpy() with a truncation length to an uninitialized stack buffer, leaving a string that was only null terminated by luck. While here, change to use no-copy semantics (no extra buffer) to avoid buffer overflows altogether. --- utils/exportfs/exportfs.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/utils/exportfs/exportfs.c b/utils/exportfs/exportfs.c index e7d1ac8..bdea12b 100644 --- a/utils/exportfs/exportfs.c +++ b/utils/exportfs/exportfs.c @@ -351,16 +351,15 @@ static int exportfs_generic(char *arg, char *options, int verbose) static int exportfs_ipv6(char *arg, char *options, int verbose) { - char *path, *c, hname[NI_MAXHOST + strlen("/128")]; + char *path, *c; arg++; c = strchr(arg, ']'); if (c == NULL) return 1; - strncpy(hname, arg, c - arg); /* no colon means this is a wildcarded DNS hostname */ - if (strchr(hname, ':') == NULL) + if (memchr(arg, ':', c - arg) == NULL) return exportfs_generic(--arg, options, verbose); path = strstr(c, ":/"); @@ -370,9 +369,9 @@ static int exportfs_ipv6(char *arg, char *options, int verbose) /* if there's anything between the closing brace and the * path separator, it's probably a prefix length */ - strcat(hname, ++c); + memmove(c, c + 1, path - c); - exportfs_parsed(hname, path, options, verbose); + exportfs_parsed(arg, path, options, verbose); return 0; } @@ -458,16 +457,15 @@ static int unexportfs_generic(char *arg, int verbose) static int unexportfs_ipv6(char *arg, int verbose) { - char *path, *c, hname[NI_MAXHOST + strlen("/128")]; + char *path, *c; arg++; c = strchr(arg, ']'); if (c == NULL) return 1; - strncpy(hname, arg, c - arg); /* no colon means this is a wildcarded DNS hostname */ - if (strchr(hname, ':') == NULL) + if (memchr(arg, ':', c - arg) == NULL) return unexportfs_generic(--arg, verbose); path = strstr(c, ":/"); @@ -477,9 +475,9 @@ static int unexportfs_ipv6(char *arg, int verbose) /* if there's anything between the closing brace and the * path separator, it's probably a prefix length */ - strcat(hname, ++c); + memmove(c, c + 1, path - c); - unexportfs_parsed(hname, path, verbose); + unexportfs_parsed(arg, path, verbose); return 0; }