From patchwork Thu Apr 2 08:32:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Praveen Paneri X-Patchwork-Id: 6145171 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 1D70EBF4A7 for ; Thu, 2 Apr 2015 08:28:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 395262035B for ; Thu, 2 Apr 2015 08:28:39 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 5735F20328 for ; Thu, 2 Apr 2015 08:28:38 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C872B6E99F; Thu, 2 Apr 2015 01:28:37 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by gabe.freedesktop.org (Postfix) with ESMTP id B9C5B6E99A for ; Thu, 2 Apr 2015 01:28:36 -0700 (PDT) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP; 02 Apr 2015 01:28:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.11,509,1422950400"; d="scan'208";a="689364138" Received: from intel-desktop.iind.intel.com ([10.223.82.37]) by fmsmga001.fm.intel.com with ESMTP; 02 Apr 2015 01:28:36 -0700 From: Praveen Paneri To: intel-gfx@lists.freedesktop.org Date: Thu, 2 Apr 2015 14:02:23 +0530 Message-Id: <1427963547-23614-9-git-send-email-praveen.paneri@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1427963547-23614-1-git-send-email-praveen.paneri@intel.com> References: <1427963547-23614-1-git-send-email-praveen.paneri@intel.com> Cc: Praveen Paneri Subject: [Intel-gfx] [PATCH 08/12] drm: Validate memory allocations X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_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 This patch adds check on various drmMalloc() calls if they were able to allocate memory as requested or not. Return appropriate error if the allocation fails. Signed-off-by: Praveen Paneri --- xf86drm.c | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index 1e25424..373113b 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -186,6 +186,8 @@ drmHashEntry *drmGetEntry(int fd) if (drmHashLookup(drmHashTable, key, &value)) { entry = drmMalloc(sizeof(*entry)); + if (!entry) + return NULL; entry->fd = fd; entry->f = NULL; entry->tagTable = drmHashCreate(); @@ -837,6 +839,8 @@ drmVersionPtr drmGetVersion(int fd) { drmVersionPtr retval; drm_version_t *version = drmMalloc(sizeof(*version)); + if (!version) + return NULL; memclear(*version); @@ -864,7 +868,9 @@ drmVersionPtr drmGetVersion(int fd) if (version->desc_len) version->desc[version->desc_len] = '\0'; retval = drmMalloc(sizeof(*retval)); - drmCopyVersion(retval, version); + if (retval) + drmCopyVersion(retval, version); + drmFreeKernelVersion(version); return retval; } @@ -886,6 +892,8 @@ drmVersionPtr drmGetVersion(int fd) drmVersionPtr drmGetLibVersion(int fd) { drm_version_t *version = drmMalloc(sizeof(*version)); + if (!version) + return NULL; /* Version history: * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it @@ -1294,14 +1302,25 @@ drmBufInfoPtr drmGetBufInfo(int fd) } retval = drmMalloc(sizeof(*retval)); + if (!retval) { + drmFree(info.list); + return NULL; + } + retval->count = info.count; retval->list = drmMalloc(info.count * sizeof(*retval->list)); - for (i = 0; i < info.count; i++) { - retval->list[i].count = info.list[i].count; - retval->list[i].size = info.list[i].size; - retval->list[i].low_mark = info.list[i].low_mark; - retval->list[i].high_mark = info.list[i].high_mark; + if (retval->list) { + for (i = 0; i < info.count; i++) { + retval->list[i].count = info.list[i].count; + retval->list[i].size = info.list[i].size; + retval->list[i].low_mark = info.list[i].low_mark; + retval->list[i].high_mark = info.list[i].high_mark; + } + } else { + drmFree(retval); + retval = NULL; } + drmFree(info.list); return retval; } @@ -1345,13 +1364,23 @@ drmBufMapPtr drmMapBufs(int fd) } retval = drmMalloc(sizeof(*retval)); + if (!retval) { + drmFree(bufs.list); + return NULL; + } + retval->count = bufs.count; retval->list = drmMalloc(bufs.count * sizeof(*retval->list)); - for (i = 0; i < bufs.count; i++) { - retval->list[i].idx = bufs.list[i].idx; - retval->list[i].total = bufs.list[i].total; - retval->list[i].used = 0; - retval->list[i].address = bufs.list[i].address; + if (retval->list) { + for (i = 0; i < bufs.count; i++) { + retval->list[i].idx = bufs.list[i].idx; + retval->list[i].total = bufs.list[i].total; + retval->list[i].used = 0; + retval->list[i].address = bufs.list[i].address; + } + } else { + drmFree(retval); + retval = NULL; } drmFree(bufs.list);