libdrm, Fix some issues from static analysis
cover libdrm, Fix some issues from static analysis
From: Tim Gore <[email protected]>
A static analysis tool reported several "critical" issues in libdrm code. This patchset addresses a subset of these in the xf86drm*** source files. Most of these issues are potential null pointer dereferences and are simply fixed by explicit null check.
Tim Gore (3):
libdrm: fix some potential security issues
libdrm: fix more potential security issues
libdrm: fix potential security issues in xf86drmSL.c
xf86drm.c | 99 ++++++++++++++++++++++++++++++++++++++--------------------- xf86drmHash.c | 12 ++++++-- xf86drmSL.c | 20 ++++++++++-- 3 files changed, 91 insertions(+), 40 deletions(-)
Signature · 1 line
1.9.2
PATCH 1/3 libdrm: fix some potential security issues
From: Tim Gore <[email protected]>
A static analysis of libdrm source code has identified several potential bugs. This commit addresses the critical issues in xf86drm.c, which are all possible null pointer dereferences.
NOTE: I have kept to the indenting style already used in this file, which is a mixture of spaces and tabs.
--- xf86drm.c | 99 +++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 35 deletions(-)
xf86drm.c +64 -35
@@ -191,12 +191,20 @@ drmHashEntry *drmGetEntry(int fd)if (!drmHashTable) drmHashTable =drmHashCreate (); +if (!drmHashTable) +return NULL ; +if (drmHashLookup (drmHashTable, key, &value)) { - entry =drmMalloc (sizeof (*entry)); - entry->fd = fd; - entry->f =NULL ; - entry->tagTable =drmHashCreate (); -drmHashInsert (drmHashTable, key, entry); +if ((entry =drmMalloc (sizeof (*entry)))) { + entry->fd = fd; + entry->f =NULL ; +if ((entry->tagTable =drmHashCreate ())) { +drmHashInsert (drmHashTable, key, entry); + }else { +drmFree (entry); + entry =NULL ; + } + } }else { entry = value; }
@@ -743,6 +751,9 @@ drmVersionPtr drmGetVersion(int fd)drmVersionPtr retval;drm_version_t *version =drmMalloc (sizeof (*version)); +if (!version) +return NULL ; + version->name_len =0 ; version->name =NULL ; version->date_len =0 ;
@@ -774,7 +785,8 @@ 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; }
@@ -797,6 +809,9 @@ 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 * revision 1.0.x = original DRM interface with no drmGetLibVersion
@@ -1112,13 +1127,14 @@ int drmClose(int fd)unsigned long key =drmGetKeyFromFd (fd);drmHashEntry *entry =drmGetEntry (fd); -drmHashDestroy (entry->tagTable); - entry->fd =0 ; - entry->f =NULL ; - entry->tagTable =NULL ; - +if (entry) { +drmHashDestroy (entry->tagTable); + entry->fd =0 ; + entry->f =NULL ; + entry->tagTable =NULL ; +drmFree (entry); + }drmHashDelete (drmHashTable, key); -drmFree (entry);return close (fd); }
@@ -1194,14 +1210,19 @@ drmBufInfoPtr drmGetBufInfo(int fd)return NULL ; } - retval =drmMalloc (sizeof (*retval)); - 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 =drmMalloc (sizeof (*retval)))) { + retval->count = info.count; +if ((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; + } + }else { +drmFree (retval); + retval =NULL ; + } }drmFree (info.list);return retval;
@@ -1247,14 +1268,19 @@ drmBufMapPtr drmMapBufs(int fd)return NULL ; } - retval =drmMalloc (sizeof (*retval)); - 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 =drmMalloc (sizeof (*retval))) { + retval->count = bufs.count; +if ((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; + } + }else { +drmFree (retval); + retval =NULL ; + } }drmFree (bufs.list);
@@ -2099,10 +2125,11 @@ int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)int drmAddContextTag (int fd,drm_context_t context,void *tag) {drmHashEntry *entry =drmGetEntry (fd); - -if (drmHashInsert (entry->tagTable, context, tag)) { -drmHashDelete (entry->tagTable, context); -drmHashInsert (entry->tagTable, context, tag); +if (entry) { +if (drmHashInsert (entry->tagTable, context, tag)) { +drmHashDelete (entry->tagTable, context); +drmHashInsert (entry->tagTable, context, tag); + } }return 0 ; }
@@ -2110,8 +2137,10 @@ int drmAddContextTag(int fd, drm_context_t context, void *tag)int drmDelContextTag (int fd,drm_context_t context) {drmHashEntry *entry =drmGetEntry (fd); - -return drmHashDelete (entry->tagTable, context); +if (entry) +return drmHashDelete (entry->tagTable, context); +else +return 1 ;/* not found */ }void *drmGetContextTag (int fd,drm_context_t context)
@@ -2119,7 +2148,7 @@ void *drmGetContextTag(int fd, drm_context_t context)drmHashEntry *entry =drmGetEntry (fd);void *value; -if (drmHashLookup (entry->tagTable, context, &value)) +if (!entry || (drmHashLookup (entry->tagTable, context, &value)))return NULL ;return value;
Signature · 1 line
1.9.2
PATCH 2/3 libdrm: fix more potential security issues
In reply to [email protected]From: Tim Gore <[email protected]>
A static analysis of libdrm source code has identified several potential bugs. This commit addresses the critical issues in xf86drmHash.c, which are all 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.
--- xf86drmHash.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
xf86drmHash.c +10 -2
@@ -91,6 +91,7 @@#define HASH_RANDOM_INIT (seed) srandom(seed)#define HASH_RANDOM random()#define HASH_RANDOM_DESTROY +#define HASH_RANDOM_OK (1)#else #define HASH_ALLOC drmMalloc#define HASH_FREE drmFree
@@ -98,6 +99,7 @@#define HASH_RANDOM_INIT (seed) state = drmRandomCreate(seed)#define HASH_RANDOM drmRandom(state)#define HASH_RANDOM_DESTROY drmRandomDestroy(state) +#define HASH_RANDOM_OK (state != NULL)#endif
@@ -137,8 +139,14 @@ static unsigned long HashHash(unsigned long key)if (!init) {HASH_RANDOM_DECL ;HASH_RANDOM_INIT (37 ); -for (i =0 ; i <256 ; i++) scatter[i] =HASH_RANDOM ; -HASH_RANDOM_DESTROY ; +if (HASH_RANDOM_OK ) { +for (i =0 ; i <256 ; i++) scatter[i] =HASH_RANDOM ; +HASH_RANDOM_DESTROY ; + }else { +/* if we failed to allocate our random number state, fall back on random() */ +srandom (37 ); +for (i =0 ; i <256 ; i++) scatter[i] =random (); + } ++init; }
Signature · 1 line
1.9.2
PATCH 3/3 libdrm: fix potential security issues in xf86drmSL.c
In reply to [email protected]From: Tim Gore <[email protected]>
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.
--- xf86drmSL.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
xf86drmSL.c +17 -3
@@ -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;
Signature · 1 line
1.9.2