src/solaris/native/sun/tools/attach/LinuxVirtualMachine.c

Print this page
rev 9543 : 8038233: Fix unsafe strcpy in Java_sun_tools_attach_{Aix,Bsd,Linux}VirtualMachine_connect()
   1 /*
   2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 139     if (fd == -1) {
 140         JNU_ThrowIOExceptionWithLastError(env, "socket");
 141     }
 142     return (jint)fd;
 143 }
 144 
 145 /*
 146  * Class:     sun_tools_attach_LinuxVirtualMachine
 147  * Method:    connect
 148  * Signature: (ILjava/lang/String;)I
 149  */
 150 JNIEXPORT void JNICALL Java_sun_tools_attach_LinuxVirtualMachine_connect
 151   (JNIEnv *env, jclass cls, jint fd, jstring path)
 152 {
 153     jboolean isCopy;
 154     const char* p = GetStringPlatformChars(env, path, &isCopy);
 155     if (p != NULL) {
 156         struct sockaddr_un addr;
 157         int err = 0;
 158 

 159         addr.sun_family = AF_UNIX;
 160         strcpy(addr.sun_path, p);

 161 
 162         if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
 163             err = errno;
 164         }
 165 
 166         if (isCopy) {
 167             JNU_ReleaseStringPlatformChars(env, path, p);
 168         }
 169 
 170         /*
 171          * If the connect failed then we throw the appropriate exception
 172          * here (can't throw it before releasing the string as can't call
 173          * JNI with pending exception)
 174          */
 175         if (err != 0) {
 176             if (err == ENOENT) {
 177                 JNU_ThrowByName(env, "java/io/FileNotFoundException", NULL);
 178             } else {
 179                 char* msg = strdup(strerror(err));
 180                 JNU_ThrowIOException(env, msg);


   1 /*
   2  * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 139     if (fd == -1) {
 140         JNU_ThrowIOExceptionWithLastError(env, "socket");
 141     }
 142     return (jint)fd;
 143 }
 144 
 145 /*
 146  * Class:     sun_tools_attach_LinuxVirtualMachine
 147  * Method:    connect
 148  * Signature: (ILjava/lang/String;)I
 149  */
 150 JNIEXPORT void JNICALL Java_sun_tools_attach_LinuxVirtualMachine_connect
 151   (JNIEnv *env, jclass cls, jint fd, jstring path)
 152 {
 153     jboolean isCopy;
 154     const char* p = GetStringPlatformChars(env, path, &isCopy);
 155     if (p != NULL) {
 156         struct sockaddr_un addr;
 157         int err = 0;
 158 
 159         memset(&addr, 0, sizeof(addr));
 160         addr.sun_family = AF_UNIX;
 161         /* strncpy is safe because addr.sun_path was zero-initialized before. */
 162         strncpy(addr.sun_path, p, sizeof(addr.sun_path) - 1);
 163 
 164         if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
 165             err = errno;
 166         }
 167 
 168         if (isCopy) {
 169             JNU_ReleaseStringPlatformChars(env, path, p);
 170         }
 171 
 172         /*
 173          * If the connect failed then we throw the appropriate exception
 174          * here (can't throw it before releasing the string as can't call
 175          * JNI with pending exception)
 176          */
 177         if (err != 0) {
 178             if (err == ENOENT) {
 179                 JNU_ThrowByName(env, "java/io/FileNotFoundException", NULL);
 180             } else {
 181                 char* msg = strdup(strerror(err));
 182                 JNU_ThrowIOException(env, msg);