src/aix/native/sun/tools/attach/AixVirtualMachine.c

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


  75         setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv));
  76         setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(tv));
  77     }
  78     return (jint)fd;
  79 }
  80 
  81 /*
  82  * Class:     sun_tools_attach_AixVirtualMachine
  83  * Method:    connect
  84  * Signature: (ILjava/lang/String;)I
  85  */
  86 JNIEXPORT void JNICALL Java_sun_tools_attach_AixVirtualMachine_connect
  87   (JNIEnv *env, jclass cls, jint fd, jstring path)
  88 {
  89     jboolean isCopy;
  90     const char* p = GetStringPlatformChars(env, path, &isCopy);
  91     if (p != NULL) {
  92         struct sockaddr_un addr;
  93         int err = 0;
  94 
  95         /* added missing structure initialization */
  96         memset(&addr,0, sizeof(addr));
  97         addr.sun_family = AF_UNIX;
  98         strcpy(addr.sun_path, p);

  99         /* We must call bind with the actual socketaddr length. This is obligatory for AS400. */
 100         if (connect(fd, (struct sockaddr*)&addr, SUN_LEN(&addr)) == -1) {
 101             err = errno;
 102         }
 103 
 104         if (isCopy) {
 105             JNU_ReleaseStringPlatformChars(env, path, p);
 106         }
 107 
 108         /*
 109          * If the connect failed then we throw the appropriate exception
 110          * here (can't throw it before releasing the string as can't call
 111          * JNI with pending exception)
 112          */
 113         if (err != 0) {
 114             if (err == ENOENT) {
 115                 JNU_ThrowByName(env, "java/io/FileNotFoundException", NULL);
 116             } else {
 117                 char* msg = strdup(strerror(err));
 118                 JNU_ThrowIOException(env, msg);


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


  75         setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv));
  76         setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(tv));
  77     }
  78     return (jint)fd;
  79 }
  80 
  81 /*
  82  * Class:     sun_tools_attach_AixVirtualMachine
  83  * Method:    connect
  84  * Signature: (ILjava/lang/String;)I
  85  */
  86 JNIEXPORT void JNICALL Java_sun_tools_attach_AixVirtualMachine_connect
  87   (JNIEnv *env, jclass cls, jint fd, jstring path)
  88 {
  89     jboolean isCopy;
  90     const char* p = GetStringPlatformChars(env, path, &isCopy);
  91     if (p != NULL) {
  92         struct sockaddr_un addr;
  93         int err = 0;
  94 
  95         memset(&addr, 0, sizeof(addr));

  96         addr.sun_family = AF_UNIX;
  97         /* strncpy is safe because addr.sun_path was zero-initialized before. */
  98         strncpy(addr.sun_path, p, sizeof(addr.sun_path) - 1);
  99         /* We must call bind with the actual socketaddr length. This is obligatory for AS400. */
 100         if (connect(fd, (struct sockaddr*)&addr, SUN_LEN(&addr)) == -1) {
 101             err = errno;
 102         }
 103 
 104         if (isCopy) {
 105             JNU_ReleaseStringPlatformChars(env, path, p);
 106         }
 107 
 108         /*
 109          * If the connect failed then we throw the appropriate exception
 110          * here (can't throw it before releasing the string as can't call
 111          * JNI with pending exception)
 112          */
 113         if (err != 0) {
 114             if (err == ENOENT) {
 115                 JNU_ThrowByName(env, "java/io/FileNotFoundException", NULL);
 116             } else {
 117                 char* msg = strdup(strerror(err));
 118                 JNU_ThrowIOException(env, msg);