From patchwork Mon Aug 6 18:17:50 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Elder X-Patchwork-Id: 1280671 Return-Path: X-Original-To: patchwork-ceph-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id C6B7940AC9 for ; Mon, 6 Aug 2012 18:17:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932356Ab2HFSRz (ORCPT ); Mon, 6 Aug 2012 14:17:55 -0400 Received: from mail-pb0-f46.google.com ([209.85.160.46]:53282 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932114Ab2HFSRw (ORCPT ); Mon, 6 Aug 2012 14:17:52 -0400 Received: by mail-pb0-f46.google.com with SMTP id rr13so2921462pbb.19 for ; Mon, 06 Aug 2012 11:17:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=dCTj/Wik60lVm+koKfq9BTFQxcP+MqSQvcJifsoUPx4=; b=fFaCWZP88ZSSoKwNFZ/dDgwHLQSg7U42OEkp+9/m9UawgFm7P966gp6uygZMI3QA6b BJzqVFn85d9dOCumRGE+dDQ/AaiQvlIl8bPjyfEjY7BU0XbWULC9lMQ2z11CNCNxrrKn ovo2mVI9krLUlOUBRzPh/oukPC26kUXSoD5brBQkI5o6DCs4ZdF+gM0pv+g/V3Vhai+7 s4pf8WJI29quxUi7UVpf4J+3ey3o4O3OxOD/TH/VtDzwWI2ZxYf95nv/TofveEvtJxVk 9vMq4ohuZIgJAhAN7JhTuTQsYyBZV7El510RyfqMTG0gLY29aU1Q6CHJk7tKNUF+M7CC mE6w== Received: by 10.68.222.103 with SMTP id ql7mr20815867pbc.48.1344277072426; Mon, 06 Aug 2012 11:17:52 -0700 (PDT) Received: from ?IPv6:2607:f298:a:607:3c9c:52cb:843e:71a9? ([2607:f298:a:607:3c9c:52cb:843e:71a9]) by mx.google.com with ESMTPS id wi6sm5976450pbc.35.2012.08.06.11.17.51 (version=SSLv3 cipher=OTHER); Mon, 06 Aug 2012 11:17:51 -0700 (PDT) Message-ID: <50200A4E.9050901@inktank.com> Date: Mon, 06 Aug 2012 11:17:50 -0700 From: Alex Elder User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: "ceph-devel@vger.kernel.org" Subject: [PATCH 3/4] rbd: expand rbd_dev_ondisk_valid() checks References: <502009D1.7090005@inktank.com> In-Reply-To: <502009D1.7090005@inktank.com> X-Gm-Message-State: ALoCoQn7h647bmnYWwMh7oDnTX3v3x48IVsEP/dOpx04MpYMX9mAyt5SFj+J1qfwG40I3SOgnOcQ Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org Add checks on the validity of the snap_count and snap_names_len field values in rbd_dev_ondisk_valid(). This eliminates the need to do them in rbd_header_from_disk(). Signed-off-by: Alex Elder Reviewed-by: Josh Durgin --- drivers/block/rbd.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Index: b/drivers/block/rbd.c =================================================================== --- a/drivers/block/rbd.c +++ b/drivers/block/rbd.c @@ -482,8 +482,31 @@ static void rbd_coll_release(struct kref static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) { - return !memcmp(&ondisk->text, - RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)); + size_t size; + u32 snap_count; + + /* The header has to start with the magic rbd header text */ + if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) + return false; + + /* + * The size of a snapshot header has to fit in a size_t, and + * that valid limits the number of snapshots. + */ + snap_count = le32_to_cpu(ondisk->snap_count); + size = SIZE_MAX - sizeof (struct ceph_snap_context); + if (snap_count > size / sizeof (__le64)) + return false; + + /* + * Not only that, but the size of the entire the snapshot + * header must also be representable in a size_t. + */ + size -= snap_count * sizeof (__le64); + if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) + return false; + + return true; } /* @@ -500,15 +523,10 @@ static int rbd_header_from_disk(struct r if (!rbd_dev_ondisk_valid(ondisk)) return -ENXIO; - snap_count = le32_to_cpu(ondisk->snap_count); - - /* Make sure we don't overflow below */ - size = SIZE_MAX - sizeof (struct ceph_snap_context); - if (snap_count > size / sizeof (header->snapc->snaps[0])) - return -EINVAL; - memset(header, 0, sizeof (*header)); + snap_count = le32_to_cpu(ondisk->snap_count); + size = sizeof (ondisk->block_name) + 1; header->object_prefix = kmalloc(size, GFP_KERNEL); if (!header->object_prefix)