Skip to content

Commit 87c80bf

Browse files
committed
Fix a double free in the List functions
The code was set up so that it would free the individual items and the data in `freeListData`, but there was already a Go `defer` to free the data item, resulting in a double free. Remove the `free` in `freeListData` and leave the original one. In addition, move the `defer` for freeing the list data before the error check, so that the data is also free in the error case. This just removes a minor leak. This vulnerability was discovered by: Jasiel Spelman of Trend Micro Zero Day Initiative and Trend Micro Team Nebula Signed-off-by: Justin Cormack <[email protected]>
1 parent 680ca48 commit 87c80bf

File tree

4 files changed

+4
-7
lines changed

4 files changed

+4
-7
lines changed

osxkeychain/osxkeychain_darwin.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,4 @@ void freeListData(char *** data, unsigned int length) {
224224
for(int i=0; i<length; i++) {
225225
free((*data)[i]);
226226
}
227-
free(*data);
228227
}

osxkeychain/osxkeychain_darwin.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ func (h Osxkeychain) List() (map[string]string, error) {
109109
defer C.free(unsafe.Pointer(acctsC))
110110
var listLenC C.uint
111111
errMsg := C.keychain_list(credsLabelC, &pathsC, &acctsC, &listLenC)
112+
defer C.freeListData(&pathsC, listLenC)
113+
defer C.freeListData(&acctsC, listLenC)
112114
if errMsg != nil {
113115
defer C.free(unsafe.Pointer(errMsg))
114116
goMsg := C.GoString(errMsg)
@@ -119,9 +121,6 @@ func (h Osxkeychain) List() (map[string]string, error) {
119121
return nil, errors.New(goMsg)
120122
}
121123

122-
defer C.freeListData(&pathsC, listLenC)
123-
defer C.freeListData(&acctsC, listLenC)
124-
125124
var listLen int
126125
listLen = int(listLenC)
127126
pathTmp := (*[1 << 30]*C.char)(unsafe.Pointer(pathsC))[:listLen:listLen]

secretservice/secretservice_linux.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,4 @@ void freeListData(char *** data, unsigned int length) {
158158
for(i=0; i<length; i++) {
159159
free((*data)[i]);
160160
}
161-
free(*data);
162161
}

secretservice/secretservice_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ func (h Secretservice) List() (map[string]string, error) {
9292
defer C.free(unsafe.Pointer(acctsC))
9393
var listLenC C.uint
9494
err := C.list(credsLabelC, &pathsC, &acctsC, &listLenC)
95+
defer C.freeListData(&pathsC, listLenC)
96+
defer C.freeListData(&acctsC, listLenC)
9597
if err != nil {
9698
defer C.g_error_free(err)
9799
return nil, errors.New("Error from list function in secretservice_linux.c likely due to error in secretservice library")
98100
}
99-
defer C.freeListData(&pathsC, listLenC)
100-
defer C.freeListData(&acctsC, listLenC)
101101

102102
resp := make(map[string]string)
103103

0 commit comments

Comments
 (0)