8038233: Fix unsafe strcpy in Java_sun_tools_attach_{Aix,Bsd,Linux}VirtualMachine_connect()
Reviewed-by: alanb
--- a/jdk/src/aix/native/sun/tools/attach/AixVirtualMachine.c Fri Mar 28 14:33:53 2014 +0400
+++ b/jdk/src/aix/native/sun/tools/attach/AixVirtualMachine.c Fri Mar 28 11:57:02 2014 +0100
@@ -1,6 +1,6 @@
/*
- * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
- * Copyright 2013 SAP AG. All rights reserved.
+ * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright 2014 SAP AG. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -92,10 +92,10 @@
struct sockaddr_un addr;
int err = 0;
- /* added missing structure initialization */
- memset(&addr,0, sizeof(addr));
+ memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
- strcpy(addr.sun_path, p);
+ /* strncpy is safe because addr.sun_path was zero-initialized before. */
+ strncpy(addr.sun_path, p, sizeof(addr.sun_path) - 1);
/* We must call bind with the actual socketaddr length. This is obligatory for AS400. */
if (connect(fd, (struct sockaddr*)&addr, SUN_LEN(&addr)) == -1) {
err = errno;
--- a/jdk/src/solaris/native/sun/tools/attach/BsdVirtualMachine.c Fri Mar 28 14:33:53 2014 +0400
+++ b/jdk/src/solaris/native/sun/tools/attach/BsdVirtualMachine.c Fri Mar 28 11:57:02 2014 +0100
@@ -78,8 +78,10 @@
struct sockaddr_un addr;
int err = 0;
+ memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
- strcpy(addr.sun_path, p);
+ /* strncpy is safe because addr.sun_path was zero-initialized before. */
+ strncpy(addr.sun_path, p, sizeof(addr.sun_path) - 1);
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
err = errno;
--- a/jdk/src/solaris/native/sun/tools/attach/LinuxVirtualMachine.c Fri Mar 28 14:33:53 2014 +0400
+++ b/jdk/src/solaris/native/sun/tools/attach/LinuxVirtualMachine.c Fri Mar 28 11:57:02 2014 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -156,8 +156,10 @@
struct sockaddr_un addr;
int err = 0;
+ memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
- strcpy(addr.sun_path, p);
+ /* strncpy is safe because addr.sun_path was zero-initialized before. */
+ strncpy(addr.sun_path, p, sizeof(addr.sun_path) - 1);
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
err = errno;