From patchwork Sat Oct 12 15:47:52 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eryu Guan X-Patchwork-Id: 3031761 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 41A9B9F3EF for ; Sat, 12 Oct 2013 15:50:44 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1E3682038B for ; Sat, 12 Oct 2013 15:50:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2336C20387 for ; Sat, 12 Oct 2013 15:50:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752395Ab3JLPua (ORCPT ); Sat, 12 Oct 2013 11:50:30 -0400 Received: from mail-pa0-f53.google.com ([209.85.220.53]:57851 "EHLO mail-pa0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752244Ab3JLPu3 (ORCPT ); Sat, 12 Oct 2013 11:50:29 -0400 Received: by mail-pa0-f53.google.com with SMTP id kq14so5682350pab.40 for ; Sat, 12 Oct 2013 08:50:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=pN9NH/pGynwdzaa+yrN3T9fn3C+jafpyABd6qU9GlWo=; b=lVfCw8R6AUyczkwkFe/n9bcAIjEfduicqm7iLlOUk1SSoXCiPCEkbEQWOCLFmnb1LI H4XeT6KrEPe/phgNqOkxgkPaZ97HO+7LEch9DomT65J1hnQr7JhCUxXOwbLGZ0/1y4hS 6VAR8BV47XPdWA7UGa5mAMHG0vrP0Uskxnqrg+WSB69ho3HC4NY4RbFX+bM2a2jGwaaM Vx27mVpj+jrX+1gK14drfisvnpf8Fs+54t0bcRWHmLr5pXX9Gz+KWgqHvL0AaT2bpCmY aI4km66DgFH5qprSiGw7lEaoityesSCSXFDgTsOrd70r9k2aNOJ0yhgQZ4V5CftelAtH W0rA== X-Received: by 10.66.50.131 with SMTP id c3mr27452618pao.111.1381593028847; Sat, 12 Oct 2013 08:50:28 -0700 (PDT) Received: from localhost ([114.253.183.54]) by mx.google.com with ESMTPSA id qw8sm66985415pbb.27.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 12 Oct 2013 08:50:28 -0700 (PDT) From: Eryu Guan To: linux-btrfs@vger.kernel.org Cc: Eryu Guan Subject: [PATCH] Btrfs-progs: check return value of realpath(3) Date: Sat, 12 Oct 2013 23:47:52 +0800 Message-Id: <1381592872-24751-1-git-send-email-guaneryu@gmail.com> X-Mailer: git-send-email 1.8.3.1 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.0 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham 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 I hit a segfault when deleting a subvolume with very long name(>4096), it's because cmd_subvol_delete() calls strdup() and passes NULL as argument, which is returned by realpath(3). I used the following script to reproduce #!/bin/bash mnt=$1 i=1 path=$mnt/subvol_$i # Create very deep subvolumes while btrfs sub create $path;do ((i++)) path="$path/subvol_$i" done last_vol=$(dirname $path) dir=$(dirname $last_vol) vol=$(basename $last_vol) # Try to delete tha last one, this would get segfault pushd $dir btrfs sub delete $vol popd Fix it by checking return value of realpath(3), also fix the one in find_mount_root(). Signed-off-by: Eryu Guan --- cmds-send.c | 8 ++++++-- cmds-subvolume.c | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cmds-send.c b/cmds-send.c index 0057e6b..9e4d031 100644 --- a/cmds-send.c +++ b/cmds-send.c @@ -62,6 +62,7 @@ int find_mount_root(const char *path, char **mount_root) int fd; struct mntent *ent; int len; + int ret; int longest_matchlen = 0; char *longest_match = NULL; @@ -91,10 +92,13 @@ int find_mount_root(const char *path, char **mount_root) return -ENOENT; } + ret = 0; *mount_root = realpath(longest_match, NULL); - free(longest_match); + if (!mount_root) + ret = -errno; - return 0; + free(longest_match); + return ret; } static int get_root_id(struct btrfs_send *s, const char *path, u64 *root_id) diff --git a/cmds-subvolume.c b/cmds-subvolume.c index ccb4762..f7249f8 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -225,6 +225,12 @@ again: } cpath = realpath(path, 0); + if (!cpath) { + ret = errno; + fprintf(stderr, "ERROR: finding real path for '%s': %s\n", + path, strerror(errno)); + goto out; + } dname = strdup(cpath); dname = dirname(dname); vname = strdup(cpath);