src/jdk.jdwp.agent/windows/native/libjdwp/linker_md.c
author afarley
Wed, 11 Sep 2019 23:10:14 +0000
changeset 58092 b00b4706ec0f
parent 47216 71c04702a3d5
permissions -rw-r--r--
8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow Summary: Check buffer overflow when the jdwp agent full dll name is built Reviewed-by: cjplummer, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 16726
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Maintains a list of currently loaded DLLs (Dynamic Link Libraries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * and their associated handles. Library names are case-insensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <errno.h>
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
    35
#include <io.h>
23566
760a74c1b589 8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents: 23010
diff changeset
    36
#include <stdlib.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "sys.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
58092
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    40
#include "util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "path_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
    43
static void dll_build_name(char* buffer, size_t buflen,
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    44
                           const char* paths, const char* fname) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    45
    char *path, *paths_copy, *next_token;
58092
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    46
    *buffer = '\0';
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
    47
58092
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    48
    paths_copy = jvmtiAllocate((int)strlen(paths) + 1);
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    49
    strcpy(paths_copy, paths);
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    50
    if (paths_copy == NULL) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    51
        return;
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    52
    }
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    53
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    54
    next_token = NULL;
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    55
    path = strtok_s(paths_copy, PATH_SEPARATOR, &next_token);
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    56
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    57
    while (path != NULL) {
58092
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    58
        size_t result_len = (size_t)_snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    59
        if (result_len >= buflen) {
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    60
            EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    61
                                                     "likely by sun.boot.library.path, is too long.");
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    62
        } else if (_access(buffer, 0) == 0) {
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
    63
            break;
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
    64
        }
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
    65
        *buffer = '\0';
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    66
        path = strtok_s(NULL, PATH_SEPARATOR, &next_token);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
    67
    }
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
    68
58092
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
    69
    jvmtiDeallocate(paths_copy);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
    70
}
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
    71
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * From system_md.c v1.54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
dbgsysGetLastErrorString(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    long errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if ((errval = GetLastError()) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        /* DOS error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    if (errno != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /* C runtime error that has no corresponding DOS error code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        const char *s = strerror(errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int n = (int)strlen(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (n >= len) n = len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        strncpy(buf, s, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Build a machine dependent library name out of a path and file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
void
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   112
dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    const int pnamelen = pname ? (int)strlen(pname) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    if (pnamelen == 0) {
58092
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
   117
        if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
   118
                EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
   119
                                                         "likely by sun.boot.library.path, is too long.");
b00b4706ec0f 8229378: jdwp library loader in linker_md.c quietly truncates on buffer overflow
afarley
parents: 47216
diff changeset
   120
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        sprintf(holder, "%s.dll", fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    } else {
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
   123
      dll_build_name(holder, holderlen, pname, fname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
dbgsysLoadLibrary(const char * name, char *err_buf, int err_buflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    void *result = LoadLibrary(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if (result == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /* Error message is pretty lame, try to make a better guess. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        long errcode = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (errcode == ERROR_MOD_NOT_FOUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            err_buf[err_buflen-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            dbgsysGetLastErrorString(err_buf, err_buflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
void dbgsysUnloadLibrary(void *handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    FreeLibrary(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
void * dbgsysFindLibraryEntry(void *handle, const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    return GetProcAddress(handle, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}