From patchwork Fri Apr 25 14:58:40 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tim.gore@intel.com X-Patchwork-Id: 4063801 Return-Path: X-Original-To: patchwork-intel-gfx@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 F380D9F387 for ; Fri, 25 Apr 2014 14:58:52 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 140F02037A for ; Fri, 25 Apr 2014 14:58:52 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 29FC3200BE for ; Fri, 25 Apr 2014 14:58:51 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id ADD666EED1; Fri, 25 Apr 2014 07:58:50 -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 [143.182.124.21]) by gabe.freedesktop.org (Postfix) with ESMTP id BBE8B6EECD for ; Fri, 25 Apr 2014 07:58:46 -0700 (PDT) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by azsmga101.ch.intel.com with ESMTP; 25 Apr 2014 07:58:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.97,927,1389772800"; d="scan'208";a="519760810" Received: from intel.iwi.intel.com ([172.28.253.39]) by fmsmga001.fm.intel.com with ESMTP; 25 Apr 2014 07:58:44 -0700 From: tim.gore@intel.com To: intel-gfx@lists.freedesktop.org Date: Fri, 25 Apr 2014 15:58:40 +0100 Message-Id: <1398437920-17394-4-git-send-email-tim.gore@intel.com> X-Mailer: git-send-email 1.9.2 In-Reply-To: <1398437920-17394-1-git-send-email-tim.gore@intel.com> References: <1398437920-17394-1-git-send-email-tim.gore@intel.com> Subject: [Intel-gfx] [PATCH 3/3] libdrm: fix potential security issues in xf86drmSL.c X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.15 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.8 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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 From: Tim Gore A static analysis of libdrm source code has identified several potential bugs. This commit addresses the critical issues in xf86drmSL.c, which are mostly potential null pointer dereferences. NOTE: I have kept to the indenting style already used in this file, which is a mixture of spaces and tabs. Signed-off-by: Tim Gore --- xf86drmSL.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/xf86drmSL.c b/xf86drmSL.c index acddb54..7af5ada 100644 --- a/xf86drmSL.c +++ b/xf86drmSL.c @@ -62,12 +62,14 @@ #define SL_RANDOM_DECL static int state = 0; #define SL_RANDOM_INIT(seed) if (!state) { srandom(seed); ++state; } #define SL_RANDOM random() +#define SL_RANDOM_OK (1) #else #define SL_ALLOC drmMalloc #define SL_FREE drmFree #define SL_RANDOM_DECL static void *state = NULL #define SL_RANDOM_INIT(seed) if (!state) state = drmRandomCreate(seed) #define SL_RANDOM drmRandom(state) +#define SL_RANDOM_OK (state != NULL) #endif @@ -124,8 +126,13 @@ static int SLRandomLevel(void) SL_RANDOM_DECL; SL_RANDOM_INIT(SL_RANDOM_SEED); - - while ((SL_RANDOM & 0x01) && level < SL_MAX_LEVEL) ++level; + if (SL_RANDOM_OK) { + while ((SL_RANDOM & 0x01) && level < SL_MAX_LEVEL) ++level; + } else { + /* if we failed to allocate our random number state, fall back on random() */ + srandom(SL_RANDOM_SEED); + while ((random() & 0x01) && level < SL_MAX_LEVEL) ++level; + } return level; } @@ -139,6 +146,10 @@ void *drmSLCreate(void) list->magic = SL_LIST_MAGIC; list->level = 0; list->head = SLCreateEntry(SL_MAX_LEVEL, 0, NULL); + if (!list->head) { + SL_FREE(list); + return NULL; + } list->count = 0; for (i = 0; i <= SL_MAX_LEVEL; i++) list->head->forward[i] = NULL; @@ -205,8 +216,9 @@ int drmSLInsert(void *l, unsigned long key, void *value) } entry = SLCreateEntry(level, key, value); + if (!entry) return -1; /* couldn't allocate a new entry */ - /* Fix up forward pointers */ + /* Fix up forward pointers */ for (i = 0; i <= level; i++) { entry->forward[i] = update[i]->forward[i]; update[i]->forward[i] = entry; @@ -270,6 +282,8 @@ int drmSLLookupNeighbors(void *l, unsigned long key, *prev_key = *next_key = key; *prev_value = *next_value = NULL; + (void) SLLocate(list, key, update); + if (update[0]) { *prev_key = update[0]->key; *prev_value = update[0]->value;