From patchwork Mon Sep 17 09:02:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Peter A. G. Crosthwaite" X-Patchwork-Id: 184334 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id ACA852C0081 for ; Mon, 17 Sep 2012 19:11:35 +1000 (EST) Received: from localhost ([::1]:60969 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDXFn-0003cy-Gh for incoming@patchwork.ozlabs.org; Mon, 17 Sep 2012 05:04:31 -0400 Received: from eggs.gnu.org ([208.118.235.92]:35653) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDXFN-00031m-5y for qemu-devel@nongnu.org; Mon, 17 Sep 2012 05:04:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TDXFD-0001ah-95 for qemu-devel@nongnu.org; Mon, 17 Sep 2012 05:04:04 -0400 Received: from mail-iy0-f173.google.com ([209.85.210.173]:53565) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDXFD-0001a3-4L for qemu-devel@nongnu.org; Mon, 17 Sep 2012 05:03:55 -0400 Received: by mail-iy0-f173.google.com with SMTP id x26so4933478iak.4 for ; Mon, 17 Sep 2012 02:03:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :in-reply-to:references:x-gm-message-state; bh=vqkXO26zb5JDaUcw7vC4D6lKihyEAbOEYkG/Rq/YNcw=; b=NLYpC/EFBbDl/UmdU+z3Gt5c/ioJfcJCTz8Z2I/zpGOYP0tSJOS6jDxvmg4eRSpNut P7tyY5+pIvXB31ysCYTigEZ2A8a4yjO+n9KH50NTr/mRPDg+KmpRBpGthMvzFVmrUkRo qU+2VHULA5x4rWq+EcTNIC1aJ/ApIh0pW8z5rPELB41BPeVOr68wd+GVyr0kQkh2EecR D9FbLP/vqX2apKhKFSBNqCJnrqRxNngJcI1wU493rJ5f0VUE5NUsCcsb78Jg47TLw3aR wR5h/6AOuvJ7iypTt69YvbWTwO5hjoK3AfcoWUrn86Wx+ygUMXecTrKkVFWY7PfvFx2d 0tcw== Received: by 10.50.140.74 with SMTP id re10mr5962707igb.52.1347872634872; Mon, 17 Sep 2012 02:03:54 -0700 (PDT) Received: from localhost ([124.148.20.9]) by mx.google.com with ESMTPS id y9sm16570976igm.10.2012.09.17.02.03.52 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 17 Sep 2012 02:03:54 -0700 (PDT) From: "Peter A. G. Crosthwaite" To: qemu-devel@nongnu.org, edgar.iglesias@gmail.com Date: Mon, 17 Sep 2012 19:02:56 +1000 Message-Id: X-Mailer: git-send-email 1.7.0.4 In-Reply-To: References: In-Reply-To: References: X-Gm-Message-State: ALoCoQmWUrGuZYG1m/0qHlV0caHJSdFfdx4V16AEgC6QcbiY+bOHGvzQEAR2wz8LXiYB1px3ZI1r X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.210.173 Cc: peter.crosthwaite@petalogix.com, crwulff@gmail.com Subject: [Qemu-devel] [RFC v0 04/10] device_tree: get_prop(): memdup returned properties X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org getprop currently return a pointer into the device tree itself. These pointers will be corrupted if the device tree is written to in anyway. To make getprop() safe, duplicate the property so that the value doesnt change over the lifetime of the returned pointer. Signed-off-by: Peter A. G. Crosthwaite --- device_tree.c | 11 +++++++---- device_tree.h | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/device_tree.c b/device_tree.c index 7edbbff..3e22286 100644 --- a/device_tree.c +++ b/device_tree.c @@ -178,7 +178,7 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path, return r; } -const void *qemu_devtree_getprop(void *fdt, const char *node_path, +void *qemu_devtree_getprop(void *fdt, const char *node_path, const char *property, int *lenp, bool inherit, Error **errp) { @@ -200,7 +200,7 @@ const void *qemu_devtree_getprop(void *fdt, const char *node_path, error_set(errp, QERR_UNDEFINED_ERROR); return NULL; } - return r; + return g_memdup(r, *lenp); } uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path, @@ -208,7 +208,8 @@ uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path, bool inherit, Error **errp) { int len; - const uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len, + uint32_t ret; + uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len, inherit, errp); if (errp && *errp) { return 0; @@ -221,7 +222,9 @@ uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path, error_set(errp, QERR_UNDEFINED_ERROR); return 0; } - return be32_to_cpu(p[offset]); + ret = be32_to_cpu(p[offset]); + g_free(p); + return ret; } uint32_t qemu_devtree_get_phandle(void *fdt, const char *path) diff --git a/device_tree.h b/device_tree.h index c3f3b28..2bc188a 100644 --- a/device_tree.h +++ b/device_tree.h @@ -31,7 +31,7 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path, int qemu_devtree_setprop_phandle(void *fdt, const char *node_path, const char *property, const char *target_node_path); -const void *qemu_devtree_getprop(void *fdt, const char *node_path, +void *qemu_devtree_getprop(void *fdt, const char *node_path, const char *property, int *lenp, bool inherit, Error **errp); uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,