Skip to contentSkip to conversation

external/drm: Fix static analysis issues

PATCH New Praveen Paneri 1 participant 10 messages Mar 6, 2015

lore
Share & tools
lore
#0 lore raw

cover external/drm: Fix static analysis issues

This patch set fixes various issues reported by a static analysis tool.

Praveen Paneri (9):
external/drm: Validate bo_fake before using.
external/drm: Validate output of realloc()
external/drm: Use snprintf instead of sprintf
external/drm: Validate pointer before using
external/drm: Validate function return value
external/drm: Avoid negative array index value
external/drm: Check function return value
external/drm: Validate all memory allocations
external/drm: Avoid uninitialized variable

 intel/intel_bufmgr_fake.c | 16 +++++++---
 intel/intel_bufmgr_gem.c  | 40 ++++++++++++++++++------
 intel/intel_decode.c      | 14 ++++++---
 xf86drm.c                 | 80 ++++++++++++++++++++++++++++++++++++-----------
 xf86drmHash.c             |  9 ++++++
 xf86drmSL.c               |  5 +++
 6 files changed, 128 insertions(+), 36 deletions(-)
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 1/9 external/drm: Validate bo_fake before using.

Check on bo_fake before dereferencing the object in functions evict_lru and evict_mru.

---
 intel/intel_bufmgr_fake.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
intel/intel_bufmgr_fake.c +8 -4
index d63fc81..ed31c23 100644
@@ -556,8 +556,10 @@ evict_lru(drm_intel_bufmgr_fake *bufmgr_fake, unsigned int max_fence)
 							    max_fence))
 			return 0;
 
-		set_dirty(&bo_fake->bo);
-		bo_fake->block = NULL;
+		if (bo_fake) {
+			set_dirty(&bo_fake->bo);
+			bo_fake->block = NULL;
+		}
 
 		free_block(bufmgr_fake, block, 0);
 		return 1;
@@ -579,8 +581,10 @@ evict_mru(drm_intel_bufmgr_fake *bufmgr_fake)
 		if (bo_fake && (bo_fake->flags & BM_NO_FENCE_SUBDATA))
 			continue;
 
-		set_dirty(&bo_fake->bo);
-		bo_fake->block = NULL;
+		if (bo_fake) {
+			set_dirty(&bo_fake->bo);
+			bo_fake->block = NULL;
+		}
 
 		free_block(bufmgr_fake, block, 0);
 		return 1;
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

PATCH 2/9 external/drm: Validate output of realloc()

In reply to Praveen Paneri

realloc will return NULL if failed to allocate the extra memory requested. Return from function if it fails.

---
 intel/intel_bufmgr_gem.c | 37 ++++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 9 deletions(-)
intel/intel_bufmgr_gem.c +28 -9
index 0c044bb..8251efd 100644
@@ -432,6 +432,8 @@ drm_intel_add_validate_buffer(drm_intel_bo *bo)
 	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int index;
+	struct drm_i915_gem_exec_object *exec_objects;
+	drm_intel_bo **exec_bos;
 
 	if (bo_gem->validate_index != -1)
 		return;
@@ -443,12 +445,20 @@ drm_intel_add_validate_buffer(drm_intel_bo *bo)
 		if (new_size == 0)
 			new_size = 5;
 
-		bufmgr_gem->exec_objects =
-		    realloc(bufmgr_gem->exec_objects,
-			    sizeof(*bufmgr_gem->exec_objects) * new_size);
-		bufmgr_gem->exec_bos =
-		    realloc(bufmgr_gem->exec_bos,
+		exec_objects = realloc(bufmgr_gem->exec_objects,
+				sizeof(*bufmgr_gem->exec_objects) * new_size);
+		if (!exec_objects)
+			return;
+
+		bufmgr_gem->exec_objects = exec_objects;
+
+		exec_bos = realloc(bufmgr_gem->exec_bos,
 			    sizeof(*bufmgr_gem->exec_bos) * new_size);
+		if (!exec_bos)
+			return;
+
+		bufmgr_gem->exec_bos = exec_bos;
+
 		bufmgr_gem->exec_size = new_size;
 	}
 
@@ -470,6 +480,8 @@ drm_intel_add_validate_buffer2(drm_intel_bo *bo, int need_fence)
 	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
 	int index;
+	struct drm_i915_gem_exec_object2 *exec2_objects;
+	drm_intel_bo **exec_bos;
 
 	if (bo_gem->validate_index != -1) {
 		if (need_fence)
@@ -485,12 +497,19 @@ drm_intel_add_validate_buffer2(drm_intel_bo *bo, int need_fence)
 		if (new_size == 0)
 			new_size = 5;
 
-		bufmgr_gem->exec2_objects =
-			realloc(bufmgr_gem->exec2_objects,
+		exec2_objects = realloc(bufmgr_gem->exec2_objects,
 				sizeof(*bufmgr_gem->exec2_objects) * new_size);
-		bufmgr_gem->exec_bos =
-			realloc(bufmgr_gem->exec_bos,
+		if (!exec2_objects)
+			return;
+
+		bufmgr_gem->exec2_objects = exec2_objects;
+
+		exec_bos = realloc(bufmgr_gem->exec_bos,
 				sizeof(*bufmgr_gem->exec_bos) * new_size);
+		if (!exec_bos)
+			return;
+
+		bufmgr_gem->exec_bos = exec_bos;
 		bufmgr_gem->exec_size = new_size;
 	}
 
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

PATCH 3/9 external/drm: Use snprintf instead of sprintf

In reply to Praveen Paneri

We must have upper bound on what we are going to write into a fixed size buffer.

---
 intel/intel_decode.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
intel/intel_decode.c +4 -4
index 61239dd..2667a7c 100644
@@ -762,7 +762,7 @@ static void i915_get_instruction_src0(uint32_t *data, int i, char *srcname)
 	char swizzle[100];
 
 	i915_get_instruction_src_name((a0 >> 7) & 0x7, src_nr, srcname);
-	sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
+	snprintf(swizzle, sizeof(swizzle), ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
 		swizzle_w);
 	if (strcmp(swizzle, ".xyzw") != 0)
 		strcat(srcname, swizzle);
@@ -780,7 +780,7 @@ static void i915_get_instruction_src1(uint32_t *data, int i, char *srcname)
 	char swizzle[100];
 
 	i915_get_instruction_src_name((a1 >> 13) & 0x7, src_nr, srcname);
-	sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
+	snprintf(swizzle, sizeof(swizzle), ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
 		swizzle_w);
 	if (strcmp(swizzle, ".xyzw") != 0)
 		strcat(srcname, swizzle);
@@ -797,7 +797,7 @@ static void i915_get_instruction_src2(uint32_t *data, int i, char *srcname)
 	char swizzle[100];
 
 	i915_get_instruction_src_name((a2 >> 21) & 0x7, src_nr, srcname);
-	sprintf(swizzle, ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
+	snprintf(swizzle, sizeof(swizzle), ".%s%s%s%s", swizzle_x, swizzle_y, swizzle_z,
 		swizzle_w);
 	if (strcmp(swizzle, ".xyzw") != 0)
 		strcat(srcname, swizzle);
@@ -926,7 +926,7 @@ i915_decode_dcl(struct drm_intel_decode *ctx, int i, char *instr_prefix)
 
 	switch ((d0 >> 19) & 0x3) {
 	case 1:
-		sprintf(dcl_mask, ".%s%s%s%s", dcl_x, dcl_y, dcl_z, dcl_w);
+		snprintf(dcl_mask, sizeof(dcl_mask), ".%s%s%s%s", dcl_x, dcl_y, dcl_z, dcl_w);
 		if (strcmp(dcl_mask, ".") == 0)
 			fprintf(out, "bad (empty) dcl mask\n");
 
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#4 lore raw

PATCH 4/9 external/drm: Validate pointer before using

In reply to Praveen Paneri

Move the dereferencing below the check for valid ctx pointer.

---
 intel/intel_decode.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
intel/intel_decode.c +3 -1
index 2667a7c..5a4fc4a 100644
@@ -3894,12 +3894,14 @@ drm_intel_decode(struct drm_intel_decode *ctx)
 	int ret;
 	unsigned int index = 0;
 	uint32_t devid;
-	int size = ctx->base_count * 4;
+	int size;
 	void *temp;
 
 	if (!ctx)
 		return;
 
+	size = ctx->base_count * 4;
+
 	/* Put a scratch page full of obviously undefined data after
 	 * the batchbuffer.  This lets us avoid a bunch of length
 	 * checking in statically sized packets.
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw

PATCH 5/9 external/drm: Validate function return value

In reply to Praveen Paneri

Return value of drmHashCreate() and drmGetEntry() functions can be NULL. It should be validated before being used.

---
 xf86drm.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
xf86drm.c +18 -6
index 73e0665..60663d4 100644
@@ -186,18 +186,23 @@ static unsigned long drmGetKeyFromFd(int fd)
 drmHashEntry *drmGetEntry(int fd)
 {
     unsigned long key = drmGetKeyFromFd(fd);
-    void          *value;
+    void          *value = NULL;
     drmHashEntry  *entry;
 
     if (!drmHashTable)
 	drmHashTable = drmHashCreate();
 
-    if (drmHashLookup(drmHashTable, key, &value)) {
+    if (drmHashTable && drmHashLookup(drmHashTable, key, &value)) {
 	entry           = drmMalloc(sizeof(*entry));
 	entry->fd       = fd;
 	entry->f        = NULL;
 	entry->tagTable = drmHashCreate();
-	drmHashInsert(drmHashTable, key, entry);
+	if (entry->tagTable) {
+		drmHashInsert(drmHashTable, key, entry);
+	} else {
+		drmFree(entry);
+		entry = NULL;
+	}
     } else {
 	entry = value;
     }
@@ -1099,6 +1104,8 @@ int drmClose(int fd)
 {
     unsigned long key    = drmGetKeyFromFd(fd);
     drmHashEntry  *entry = drmGetEntry(fd);
+    if(!entry)
+	return -ENOMEM;
 
     drmHashDestroy(entry->tagTable);
     entry->fd       = 0;
@@ -2088,7 +2095,7 @@ int drmAddContextTag(int fd, drm_context_t context, void *tag)
 {
     drmHashEntry  *entry = drmGetEntry(fd);
 
-    if (drmHashInsert(entry->tagTable, context, tag)) {
+    if (entry && drmHashInsert(entry->tagTable, context, tag)) {
 	drmHashDelete(entry->tagTable, context);
 	drmHashInsert(entry->tagTable, context, tag);
     }
@@ -2099,13 +2106,18 @@ int drmDelContextTag(int fd, drm_context_t context)
 {
     drmHashEntry  *entry = drmGetEntry(fd);
 
-    return drmHashDelete(entry->tagTable, context);
+    if (entry)
+	return drmHashDelete(entry->tagTable, context);
+    return -ENOMEM;
 }
 
 void *drmGetContextTag(int fd, drm_context_t context)
 {
-    drmHashEntry  *entry = drmGetEntry(fd);
     void          *value;
+    drmHashEntry  *entry = drmGetEntry(fd);
+
+    if (!entry)
+        return NULL;
 
     if (drmHashLookup(entry->tagTable, context, &value))
 	return NULL;
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#6 lore raw

PATCH 6/9 external/drm: Avoid negative array index value

In reply to Praveen Paneri

recode can be negative or zero as well. Put the correct condition on it before using it as array index.

---
 xf86drm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xf86drm.c +1 -1
index 60663d4..df56f9e 100644
@@ -585,7 +585,7 @@ static int drmOpenByName(const char *name)
 	if ((fd = open(proc_name, 0, 0)) >= 0) {
 	    retcode = read(fd, buf, sizeof(buf)-1);
 	    close(fd);
-	    if (retcode) {
+	    if (retcode > 0) {
 		buf[retcode-1] = '\0';
 		for (driver = pt = buf; *pt && *pt != ' '; ++pt)
 		    ;
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#7 lore raw

PATCH 7/9 external/drm: Check function return value

In reply to Praveen Paneri

Validate the return value of SLCreateEntry() before using it.

---
 xf86drmSL.c | 2 ++
 1 file changed, 2 insertions(+)
xf86drmSL.c +2 -0
index acddb54..45f3906 100644
@@ -139,6 +139,7 @@ void *drmSLCreate(void)
     list->magic    = SL_LIST_MAGIC;
     list->level    = 0;
     list->head     = SLCreateEntry(SL_MAX_LEVEL, 0, NULL);
+    if (!list->head) return NULL;
     list->count    = 0;
 
     for (i = 0; i <= SL_MAX_LEVEL; i++) list->head->forward[i] = NULL;
@@ -205,6 +206,7 @@ int drmSLInsert(void *l, unsigned long key, void *value)
     }
 
     entry = SLCreateEntry(level, key, value);
+    if (!entry) return -ENOMEM;
 
 				/* Fix up forward pointers */
     for (i = 0; i <= level; i++) {
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#8 lore raw

PATCH 8/9 external/drm: Validate all memory allocations

In reply to Praveen Paneri

This patch adds check for various malloc/calloc function if they were able to allocate memory as requested or not. Return appropriate error if the allocation fails.

---
 intel/intel_bufmgr_fake.c |  4 ++++
 intel/intel_bufmgr_gem.c  |  3 +++
 intel/intel_decode.c      |  2 ++
 xf86drm.c                 | 54 +++++++++++++++++++++++++++++++++++++----------
 xf86drmHash.c             |  9 ++++++++
 xf86drmSL.c               |  2 ++
 6 files changed, 63 insertions(+), 11 deletions(-)
intel/intel_bufmgr_fake.c +4 -0
index ed31c23..c00aa80 100644
@@ -1277,6 +1277,8 @@ drm_intel_fake_emit_reloc(drm_intel_bo *bo, uint32_t offset,
 	if (bo_fake->relocs == NULL) {
 		bo_fake->relocs =
 		    malloc(sizeof(struct fake_buffer_reloc) * MAX_RELOCS);
+		if (!bo_fake->relocs)
+			return -ENOMEM;
 	}
 
 	r = &bo_fake->relocs[bo_fake->nr_relocs++];
@@ -1596,6 +1598,8 @@ drm_intel_bufmgr *drm_intel_bufmgr_fake_init(int fd,
 	drm_intel_bufmgr_fake *bufmgr_fake;
 
 	bufmgr_fake = calloc(1, sizeof(*bufmgr_fake));
+	if (!bufmgr_fake)
+		return NULL;
 
 	if (pthread_mutex_init(&bufmgr_fake->lock, NULL) != 0) {
 		free(bufmgr_fake);
intel/intel_bufmgr_gem.c +3 -0
index 8251efd..5bf33c4 100644
@@ -2261,6 +2261,9 @@ aub_write_bo_data(drm_intel_bo *bo, uint32_t offset, uint32_t size)
 	unsigned int i;
 
 	data = malloc(bo->size);
+	if (!data)
+		return;
+
 	drm_intel_bo_get_subdata(bo, offset, size, data);
 
 	/* Easy mode: write out bo with no relocations */
intel/intel_decode.c +2 -0
index 5a4fc4a..015ef20 100644
@@ -3907,6 +3907,8 @@ drm_intel_decode(struct drm_intel_decode *ctx)
 	 * checking in statically sized packets.
 	 */
 	temp = malloc(size + 4096);
+	if (!temp)
+		return;
 	memcpy(temp, ctx->base_data, size);
 	memset((char *)temp + size, 0xd0, 4096);
 	ctx->data = temp;
xf86drm.c +43 -11

101 lines not on this page · raw message

xf86drmHash.c +9 -0
index 82cbc2a..56e9af3 100644
@@ -70,6 +70,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <errno.h>
 
 #define HASH_MAIN 0
 
@@ -79,6 +80,7 @@
 
 #define HASH_MAGIC 0xdeadbeef
 #define HASH_DEBUG 0
+#define HASH_INVALID 0xffffffff	/* A value that is out of bound */
 #define HASH_SIZE  512		/* Good for about 100 entries */
 				/* If you change this value, you probably
                                    have to change the HashHash hashing
@@ -137,6 +139,8 @@ static unsigned long HashHash(unsigned long key)
     if (!init) {
 	HASH_RANDOM_DECL;
 	HASH_RANDOM_INIT(37);
+	if (!state)
+		return HASH_INVALID;
 	for (i = 0; i < 256; i++) scatter[i] = HASH_RANDOM;
 	HASH_RANDOM_DESTROY;
 	++init;
@@ -203,6 +207,9 @@ static HashBucketPtr HashFind(HashTablePtr table,
 
     if (h) *h = hash;
 
+    if (hash == HASH_INVALID)
+	return NULL;
+
     for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) {
 	if (bucket->key == key) {
 	    if (prev) {
@@ -244,6 +251,7 @@ int drmHashInsert(void *t, unsigned long key, void *value)
     if (table->magic != HASH_MAGIC) return -1; /* Bad magic */
 
     if (HashFind(table, key, &hash)) return 1; /* Already in table */
+    if (hash == HASH_INVALID) return -1;
 
     bucket               = HASH_ALLOC(sizeof(*bucket));
     if (!bucket) return -1;	/* Error */
@@ -267,6 +275,7 @@ int drmHashDelete(void *t, unsigned long key)
 
     bucket = HashFind(table, key, &hash);
 
+    if (hash == HASH_INVALID) return -1;
     if (!bucket) return 1;	/* Not found */
 
     table->buckets[hash] = bucket->next;
xf86drmSL.c +2 -0
index 45f3906..00ae9f9 100644
@@ -40,6 +40,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <errno.h>
 
 #define SL_MAIN 0
 
@@ -124,6 +125,7 @@ static int SLRandomLevel(void)
     SL_RANDOM_DECL;
 
     SL_RANDOM_INIT(SL_RANDOM_SEED);
+    if (!state) return -ENOMEM;
     
     while ((SL_RANDOM & 0x01) && level < SL_MAX_LEVEL) ++level;
     return level;
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#9 lore raw

PATCH 9/9 external/drm: Avoid uninitialized variable

In reply to Praveen Paneri

drmSLLookupNeighbors function is using update without initializing it. Fixed it by calling SLLocate().

---
 xf86drmSL.c | 1 +
 1 file changed, 1 insertion(+)
xf86drmSL.c +1 -0
index 00ae9f9..ca54148 100644
@@ -271,6 +271,7 @@ int drmSLLookupNeighbors(void *l, unsigned long key,
     SLEntryPtr    update[SL_MAX_LEVEL + 1];
     int           retcode = 0;
 
+    SLLocate(list, key, update);
     *prev_key   = *next_key   = key;
     *prev_value = *next_value = NULL;
 	
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx