Skip to content

Commit d57af04

Browse files
author
Adam Farley
committed
8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
Check buffer overflow when the jdwp agent full dll name is built Reviewed-by: cjplummer, sspitsyn
1 parent a8ea6b2 commit d57af04

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/jdk.jdwp.agent/unix/native/libjdwp/linker_md.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <stdlib.h>
3535
#include <string.h>
3636

37+
#include "util.h"
3738
#include "path_md.h"
3839

3940
#ifdef __APPLE__
@@ -45,8 +46,10 @@
4546
static void dll_build_name(char* buffer, size_t buflen,
4647
const char* paths, const char* fname) {
4748
char *path, *paths_copy, *next_token;
49+
*buffer = '\0';
4850

49-
paths_copy = strdup(paths);
51+
paths_copy = jvmtiAllocate((int)strlen(paths) + 1);
52+
strcpy(paths_copy, paths);
5053
if (paths_copy == NULL) {
5154
return;
5255
}
@@ -55,15 +58,18 @@ static void dll_build_name(char* buffer, size_t buflen,
5558
path = strtok_r(paths_copy, PATH_SEPARATOR, &next_token);
5659

5760
while (path != NULL) {
58-
snprintf(buffer, buflen, "%s/lib%s." LIB_SUFFIX, path, fname);
59-
if (access(buffer, F_OK) == 0) {
61+
size_t result_len = (size_t)snprintf(buffer, buflen, "%s/lib%s." LIB_SUFFIX, path, fname);
62+
if (result_len >= buflen) {
63+
EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
64+
"likely by sun.boot.library.path, is too long.");
65+
} else if (access(buffer, F_OK) == 0) {
6066
break;
6167
}
6268
*buffer = '\0';
6369
path = strtok_r(NULL, PATH_SEPARATOR, &next_token);
6470
}
6571

66-
free(paths_copy);
72+
jvmtiDeallocate(paths_copy);
6773
}
6874

6975
/*
@@ -89,13 +95,11 @@ dbgsysBuildLibName(char *holder, int holderlen, const char *pname,
8995
{
9096
const int pnamelen = pname ? strlen(pname) : 0;
9197

92-
*holder = '\0';
93-
// Quietly truncate on buffer overflow. Should be an error.
94-
if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
95-
return;
96-
}
97-
9898
if (pnamelen == 0) {
99+
if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
100+
EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
101+
"likely by sun.boot.library.path, is too long.");
102+
}
99103
(void)snprintf(holder, holderlen, "lib%s." LIB_SUFFIX, fname);
100104
} else {
101105
dll_build_name(holder, holderlen, pname, fname);

src/jdk.jdwp.agent/windows/native/libjdwp/linker_md.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@
3737

3838
#include "sys.h"
3939

40+
#include "util.h"
4041
#include "path_md.h"
4142

4243
static void dll_build_name(char* buffer, size_t buflen,
4344
const char* paths, const char* fname) {
4445
char *path, *paths_copy, *next_token;
46+
*buffer = '\0';
4547

46-
paths_copy = strdup(paths);
48+
paths_copy = jvmtiAllocate((int)strlen(paths) + 1);
49+
strcpy(paths_copy, paths);
4750
if (paths_copy == NULL) {
4851
return;
4952
}
@@ -52,15 +55,18 @@ static void dll_build_name(char* buffer, size_t buflen,
5255
path = strtok_s(paths_copy, PATH_SEPARATOR, &next_token);
5356

5457
while (path != NULL) {
55-
_snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
56-
if (_access(buffer, 0) == 0) {
58+
size_t result_len = (size_t)_snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
59+
if (result_len >= buflen) {
60+
EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
61+
"likely by sun.boot.library.path, is too long.");
62+
} else if (_access(buffer, 0) == 0) {
5763
break;
5864
}
5965
*buffer = '\0';
6066
path = strtok_s(NULL, PATH_SEPARATOR, &next_token);
6167
}
6268

63-
free(paths_copy);
69+
jvmtiDeallocate(paths_copy);
6470
}
6571

6672
/*
@@ -107,13 +113,11 @@ dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *f
107113
{
108114
const int pnamelen = pname ? (int)strlen(pname) : 0;
109115

110-
*holder = '\0';
111-
/* Quietly truncates on buffer overflow. Should be an error. */
112-
if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
113-
return;
114-
}
115-
116116
if (pnamelen == 0) {
117+
if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
118+
EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
119+
"likely by sun.boot.library.path, is too long.");
120+
}
117121
sprintf(holder, "%s.dll", fname);
118122
} else {
119123
dll_build_name(holder, holderlen, pname, fname);

0 commit comments

Comments
 (0)