1 /*
2  * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
3  * Copyright 2012, 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.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 #include "precompiled.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "oops/oop.inline.hpp"
31 #include "os_aix.inline.hpp"
32 #include "runtime/handles.inline.hpp"
33 #include "runtime/perfMemory.hpp"
34 #include "services/memTracker.hpp"
35 #include "utilities/exceptions.hpp"
36 
37 // put OS-includes here
38 # include <sys/types.h>
39 # include <sys/mman.h>
40 # include <errno.h>
41 # include <stdio.h>
42 # include <unistd.h>
43 # include <sys/stat.h>
44 # include <signal.h>
45 # include <pwd.h>
46 
47 static char* backing_store_file_name = NULL;  // name of the backing store
48                                               // file, if successfully created.
49 
50 // Standard Memory Implementation Details
51 
52 // create the PerfData memory region in standard memory.
53 //
create_standard_memory(size_t size)54 static char* create_standard_memory(size_t size) {
55 
56   // allocate an aligned chuck of memory
57   char* mapAddress = os::reserve_memory(size);
58 
59   if (mapAddress == NULL) {
60     return NULL;
61   }
62 
63   // commit memory
64   if (!os::commit_memory(mapAddress, size, !ExecMem)) {
65     if (PrintMiscellaneous && Verbose) {
66       warning("Could not commit PerfData memory\n");
67     }
68     os::release_memory(mapAddress, size);
69     return NULL;
70   }
71 
72   return mapAddress;
73 }
74 
75 // delete the PerfData memory region
76 //
delete_standard_memory(char * addr,size_t size)77 static void delete_standard_memory(char* addr, size_t size) {
78 
79   // there are no persistent external resources to cleanup for standard
80   // memory. since DestroyJavaVM does not support unloading of the JVM,
81   // cleanup of the memory resource is not performed. The memory will be
82   // reclaimed by the OS upon termination of the process.
83   //
84   return;
85 }
86 
87 // save the specified memory region to the given file
88 //
89 // Note: this function might be called from signal handler (by os::abort()),
90 // don't allocate heap memory.
91 //
save_memory_to_file(char * addr,size_t size)92 static void save_memory_to_file(char* addr, size_t size) {
93 
94   const char* destfile = PerfMemory::get_perfdata_file_path();
95   assert(destfile[0] != '\0', "invalid PerfData file path");
96 
97   int result;
98 
99   RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
100               result);;
101   if (result == OS_ERR) {
102     if (PrintMiscellaneous && Verbose) {
103       warning("Could not create Perfdata save file: %s: %s\n",
104               destfile, strerror(errno));
105     }
106   } else {
107     int fd = result;
108 
109     for (size_t remaining = size; remaining > 0;) {
110 
111       RESTARTABLE(::write(fd, addr, remaining), result);
112       if (result == OS_ERR) {
113         if (PrintMiscellaneous && Verbose) {
114           warning("Could not write Perfdata save file: %s: %s\n",
115                   destfile, strerror(errno));
116         }
117         break;
118       }
119 
120       remaining -= (size_t)result;
121       addr += result;
122     }
123 
124     RESTARTABLE(::close(fd), result);
125     if (PrintMiscellaneous && Verbose) {
126       if (result == OS_ERR) {
127         warning("Could not close %s: %s\n", destfile, strerror(errno));
128       }
129     }
130   }
131   FREE_C_HEAP_ARRAY(char, destfile, mtInternal);
132 }
133 
134 
135 // Shared Memory Implementation Details
136 
137 // Note: the solaris and linux shared memory implementation uses the mmap
138 // interface with a backing store file to implement named shared memory.
139 // Using the file system as the name space for shared memory allows a
140 // common name space to be supported across a variety of platforms. It
141 // also provides a name space that Java applications can deal with through
142 // simple file apis.
143 //
144 // The solaris and linux implementations store the backing store file in
145 // a user specific temporary directory located in the /tmp file system,
146 // which is always a local file system and is sometimes a RAM based file
147 // system.
148 
149 // return the user specific temporary directory name.
150 //
151 // the caller is expected to free the allocated memory.
152 //
get_user_tmp_dir(const char * user)153 static char* get_user_tmp_dir(const char* user) {
154 
155   const char* tmpdir = os::get_temp_directory();
156   const char* perfdir = PERFDATA_NAME;
157   size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3;
158   char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
159 
160   // construct the path name to user specific tmp directory
161   snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user);
162 
163   return dirname;
164 }
165 
166 // convert the given file name into a process id. if the file
167 // does not meet the file naming constraints, return 0.
168 //
filename_to_pid(const char * filename)169 static pid_t filename_to_pid(const char* filename) {
170 
171   // a filename that doesn't begin with a digit is not a
172   // candidate for conversion.
173   //
174   if (!isdigit(*filename)) {
175     return 0;
176   }
177 
178   // check if file name can be converted to an integer without
179   // any leftover characters.
180   //
181   char* remainder = NULL;
182   errno = 0;
183   pid_t pid = (pid_t)strtol(filename, &remainder, 10);
184 
185   if (errno != 0) {
186     return 0;
187   }
188 
189   // check for left over characters. If any, then the filename is
190   // not a candidate for conversion.
191   //
192   if (remainder != NULL && *remainder != '\0') {
193     return 0;
194   }
195 
196   // successful conversion, return the pid
197   return pid;
198 }
199 
200 // Check if the given statbuf is considered a secure directory for
201 // the backing store files. Returns true if the directory is considered
202 // a secure location. Returns false if the statbuf is a symbolic link or
203 // if an error occurred.
204 //
is_statbuf_secure(struct stat * statp)205 static bool is_statbuf_secure(struct stat *statp) {
206   if (S_ISLNK(statp->st_mode) || !S_ISDIR(statp->st_mode)) {
207     // The path represents a link or some non-directory file type,
208     // which is not what we expected. Declare it insecure.
209     //
210     return false;
211   }
212   // We have an existing directory, check if the permissions are safe.
213   //
214   if ((statp->st_mode & (S_IWGRP|S_IWOTH)) != 0) {
215     // The directory is open for writing and could be subjected
216     // to a symlink or a hard link attack. Declare it insecure.
217     //
218     return false;
219   }
220   // If user is not root then see if the uid of the directory matches the effective uid of the process.
221   uid_t euid = geteuid();
222   if ((euid != 0) && (statp->st_uid != euid)) {
223     // The directory was not created by this user, declare it insecure.
224     //
225     return false;
226   }
227   return true;
228 }
229 
230 
231 // Check if the given path is considered a secure directory for
232 // the backing store files. Returns true if the directory exists
233 // and is considered a secure location. Returns false if the path
234 // is a symbolic link or if an error occurred.
235 //
is_directory_secure(const char * path)236 static bool is_directory_secure(const char* path) {
237   struct stat statbuf;
238   int result = 0;
239 
240   RESTARTABLE(::lstat(path, &statbuf), result);
241   if (result == OS_ERR) {
242     return false;
243   }
244 
245   // The path exists, see if it is secure.
246   return is_statbuf_secure(&statbuf);
247 }
248 
249 // (Taken over from Solaris to support the O_NOFOLLOW case on AIX.)
250 // Check if the given directory file descriptor is considered a secure
251 // directory for the backing store files. Returns true if the directory
252 // exists and is considered a secure location. Returns false if the path
253 // is a symbolic link or if an error occurred.
is_dirfd_secure(int dir_fd)254 static bool is_dirfd_secure(int dir_fd) {
255   struct stat statbuf;
256   int result = 0;
257 
258   RESTARTABLE(::fstat(dir_fd, &statbuf), result);
259   if (result == OS_ERR) {
260     return false;
261   }
262 
263   // The path exists, now check its mode.
264   return is_statbuf_secure(&statbuf);
265 }
266 
267 
268 // Check to make sure fd1 and fd2 are referencing the same file system object.
is_same_fsobject(int fd1,int fd2)269 static bool is_same_fsobject(int fd1, int fd2) {
270   struct stat statbuf1;
271   struct stat statbuf2;
272   int result = 0;
273 
274   RESTARTABLE(::fstat(fd1, &statbuf1), result);
275   if (result == OS_ERR) {
276     return false;
277   }
278   RESTARTABLE(::fstat(fd2, &statbuf2), result);
279   if (result == OS_ERR) {
280     return false;
281   }
282 
283   if ((statbuf1.st_ino == statbuf2.st_ino) &&
284       (statbuf1.st_dev == statbuf2.st_dev)) {
285     return true;
286   } else {
287     return false;
288   }
289 }
290 
291 // Helper functions for open without O_NOFOLLOW which is not present on AIX 5.3/6.1.
292 // We use the jdk6 implementation here.
293 #ifndef O_NOFOLLOW
294 // The O_NOFOLLOW oflag doesn't exist before solaris 5.10, this is to simulate that behaviour
295 // was done in jdk 5/6 hotspot by Oracle this way
open_o_nofollow_impl(const char * path,int oflag,mode_t mode,bool use_mode)296 static int open_o_nofollow_impl(const char* path, int oflag, mode_t mode, bool use_mode) {
297   struct stat orig_st;
298   struct stat new_st;
299   bool create;
300   int error;
301   int fd;
302 
303   create = false;
304 
305   if (lstat(path, &orig_st) != 0) {
306     if (errno == ENOENT && (oflag & O_CREAT) != 0) {
307       // File doesn't exist, but_we want to create it, add O_EXCL flag
308       // to make sure no-one creates it (or a symlink) before us
309       // This works as we expect with symlinks, from posix man page:
310       // 'If O_EXCL  and  O_CREAT  are set, and path names a symbolic
311       // link, open() shall fail and set errno to [EEXIST]'.
312       oflag |= O_EXCL;
313       create = true;
314     } else {
315       // File doesn't exist, and we are not creating it.
316       return OS_ERR;
317     }
318   } else {
319     // Lstat success, check if existing file is a link.
320     if ((orig_st.st_mode & S_IFMT) == S_IFLNK)  {
321       // File is a symlink.
322       errno = ELOOP;
323       return OS_ERR;
324     }
325   }
326 
327   if (use_mode == true) {
328     fd = open(path, oflag, mode);
329   } else {
330     fd = open(path, oflag);
331   }
332 
333   if (fd == OS_ERR) {
334     return fd;
335   }
336 
337   // Can't do inode checks on before/after if we created the file.
338   if (create == false) {
339     if (fstat(fd, &new_st) != 0) {
340       // Keep errno from fstat, in case close also fails.
341       error = errno;
342       ::close(fd);
343       errno = error;
344       return OS_ERR;
345     }
346 
347     if (orig_st.st_dev != new_st.st_dev || orig_st.st_ino != new_st.st_ino) {
348       // File was tampered with during race window.
349       ::close(fd);
350       errno = EEXIST;
351       if (PrintMiscellaneous && Verbose) {
352         warning("possible file tampering attempt detected when opening %s", path);
353       }
354       return OS_ERR;
355     }
356   }
357 
358   return fd;
359 }
360 
open_o_nofollow(const char * path,int oflag,mode_t mode)361 static int open_o_nofollow(const char* path, int oflag, mode_t mode) {
362   return open_o_nofollow_impl(path, oflag, mode, true);
363 }
364 
open_o_nofollow(const char * path,int oflag)365 static int open_o_nofollow(const char* path, int oflag) {
366   return open_o_nofollow_impl(path, oflag, 0, false);
367 }
368 #endif
369 
370 // Open the directory of the given path and validate it.
371 // Return a DIR * of the open directory.
open_directory_secure(const char * dirname)372 static DIR *open_directory_secure(const char* dirname) {
373   // Open the directory using open() so that it can be verified
374   // to be secure by calling is_dirfd_secure(), opendir() and then check
375   // to see if they are the same file system object.  This method does not
376   // introduce a window of opportunity for the directory to be attacked that
377   // calling opendir() and is_directory_secure() does.
378   int result;
379   DIR *dirp = NULL;
380 
381   // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
382   // so provide a workaround in this case.
383 #ifdef O_NOFOLLOW
384   RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result);
385 #else
386   // workaround (jdk6 coding)
387   RESTARTABLE(::open_o_nofollow(dirname, O_RDONLY), result);
388 #endif
389 
390   if (result == OS_ERR) {
391     // Directory doesn't exist or is a symlink, so there is nothing to cleanup.
392     if (PrintMiscellaneous && Verbose) {
393       if (errno == ELOOP) {
394         warning("directory %s is a symlink and is not secure\n", dirname);
395       } else {
396         warning("could not open directory %s: %s\n", dirname, strerror(errno));
397       }
398     }
399     return dirp;
400   }
401   int fd = result;
402 
403   // Determine if the open directory is secure.
404   if (!is_dirfd_secure(fd)) {
405     // The directory is not a secure directory.
406     os::close(fd);
407     return dirp;
408   }
409 
410   // Open the directory.
411   dirp = ::opendir(dirname);
412   if (dirp == NULL) {
413     // The directory doesn't exist, close fd and return.
414     os::close(fd);
415     return dirp;
416   }
417 
418   // Check to make sure fd and dirp are referencing the same file system object.
419   if (!is_same_fsobject(fd, dirp->dd_fd)) {
420     // The directory is not secure.
421     os::close(fd);
422     os::closedir(dirp);
423     dirp = NULL;
424     return dirp;
425   }
426 
427   // Close initial open now that we know directory is secure
428   os::close(fd);
429 
430   return dirp;
431 }
432 
433 // NOTE: The code below uses fchdir(), open() and unlink() because
434 // fdopendir(), openat() and unlinkat() are not supported on all
435 // versions.  Once the support for fdopendir(), openat() and unlinkat()
436 // is available on all supported versions the code can be changed
437 // to use these functions.
438 
439 // Open the directory of the given path, validate it and set the
440 // current working directory to it.
441 // Return a DIR * of the open directory and the saved cwd fd.
442 //
open_directory_secure_cwd(const char * dirname,int * saved_cwd_fd)443 static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) {
444 
445   // Open the directory.
446   DIR* dirp = open_directory_secure(dirname);
447   if (dirp == NULL) {
448     // Directory doesn't exist or is insecure, so there is nothing to cleanup.
449     return dirp;
450   }
451   int fd = dirp->dd_fd;
452 
453   // Open a fd to the cwd and save it off.
454   int result;
455   RESTARTABLE(::open(".", O_RDONLY), result);
456   if (result == OS_ERR) {
457     *saved_cwd_fd = -1;
458   } else {
459     *saved_cwd_fd = result;
460   }
461 
462   // Set the current directory to dirname by using the fd of the directory and
463   // handle errors, otherwise shared memory files will be created in cwd.
464   result = fchdir(fd);
465   if (result == OS_ERR) {
466     if (PrintMiscellaneous && Verbose) {
467       warning("could not change to directory %s", dirname);
468     }
469     if (*saved_cwd_fd != -1) {
470       ::close(*saved_cwd_fd);
471       *saved_cwd_fd = -1;
472     }
473     // Close the directory.
474     os::closedir(dirp);
475     return NULL;
476   } else {
477     return dirp;
478   }
479 }
480 
481 // Close the directory and restore the current working directory.
482 //
close_directory_secure_cwd(DIR * dirp,int saved_cwd_fd)483 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
484 
485   int result;
486   // If we have a saved cwd change back to it and close the fd.
487   if (saved_cwd_fd != -1) {
488     result = fchdir(saved_cwd_fd);
489     ::close(saved_cwd_fd);
490   }
491 
492   // Close the directory.
493   os::closedir(dirp);
494 }
495 
496 // Check if the given file descriptor is considered a secure.
is_file_secure(int fd,const char * filename)497 static bool is_file_secure(int fd, const char *filename) {
498 
499   int result;
500   struct stat statbuf;
501 
502   // Determine if the file is secure.
503   RESTARTABLE(::fstat(fd, &statbuf), result);
504   if (result == OS_ERR) {
505     if (PrintMiscellaneous && Verbose) {
506       warning("fstat failed on %s: %s\n", filename, strerror(errno));
507     }
508     return false;
509   }
510   if (statbuf.st_nlink > 1) {
511     // A file with multiple links is not expected.
512     if (PrintMiscellaneous && Verbose) {
513       warning("file %s has multiple links\n", filename);
514     }
515     return false;
516   }
517   return true;
518 }
519 
520 // Return the user name for the given user id.
521 //
522 // The caller is expected to free the allocated memory.
get_user_name(uid_t uid)523 static char* get_user_name(uid_t uid) {
524 
525   struct passwd pwent;
526 
527   // Determine the max pwbuf size from sysconf, and hardcode
528   // a default if this not available through sysconf.
529   long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
530   if (bufsize == -1)
531     bufsize = 1024;
532 
533   char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
534 
535   // POSIX interface to getpwuid_r is used on LINUX
536   struct passwd* p;
537   int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p);
538 
539   if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
540     if (PrintMiscellaneous && Verbose) {
541       if (result != 0) {
542         warning("Could not retrieve passwd entry: %s\n",
543                 strerror(result));
544       }
545       else if (p == NULL) {
546         // this check is added to protect against an observed problem
547         // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
548         // indicating success, but has p == NULL. This was observed when
549         // inserting a file descriptor exhaustion fault prior to the call
550         // getpwuid_r() call. In this case, error is set to the appropriate
551         // error condition, but this is undocumented behavior. This check
552         // is safe under any condition, but the use of errno in the output
553         // message may result in an erroneous message.
554         // Bug Id 89052 was opened with RedHat.
555         //
556         warning("Could not retrieve passwd entry: %s\n",
557                 strerror(errno));
558       }
559       else {
560         warning("Could not determine user name: %s\n",
561                 p->pw_name == NULL ? "pw_name = NULL" :
562                                      "pw_name zero length");
563       }
564     }
565     FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
566     return NULL;
567   }
568 
569   char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
570   strcpy(user_name, p->pw_name);
571 
572   FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
573   return user_name;
574 }
575 
576 // return the name of the user that owns the process identified by vmid.
577 //
578 // This method uses a slow directory search algorithm to find the backing
579 // store file for the specified vmid and returns the user name, as determined
580 // by the user name suffix of the hsperfdata_<username> directory name.
581 //
582 // the caller is expected to free the allocated memory.
583 //
get_user_name_slow(int vmid,TRAPS)584 static char* get_user_name_slow(int vmid, TRAPS) {
585 
586   // short circuit the directory search if the process doesn't even exist.
587   if (kill(vmid, 0) == OS_ERR) {
588     if (errno == ESRCH) {
589       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
590                   "Process not found");
591     }
592     else /* EPERM */ {
593       THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
594     }
595   }
596 
597   // directory search
598   char* oldest_user = NULL;
599   time_t oldest_ctime = 0;
600 
601   const char* tmpdirname = os::get_temp_directory();
602 
603   DIR* tmpdirp = os::opendir(tmpdirname);
604 
605   if (tmpdirp == NULL) {
606     return NULL;
607   }
608 
609   // for each entry in the directory that matches the pattern hsperfdata_*,
610   // open the directory and check if the file for the given vmid exists.
611   // The file with the expected name and the latest creation date is used
612   // to determine the user name for the process id.
613   //
614   struct dirent* dentry;
615   errno = 0;
616   while ((dentry = os::readdir(tmpdirp)) != NULL) {
617 
618     // check if the directory entry is a hsperfdata file
619     if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
620       continue;
621     }
622 
623     char* usrdir_name = NEW_C_HEAP_ARRAY(char,
624                               strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
625     strcpy(usrdir_name, tmpdirname);
626     strcat(usrdir_name, "/");
627     strcat(usrdir_name, dentry->d_name);
628 
629     // Open the user directory.
630     DIR* subdirp = open_directory_secure(usrdir_name);
631 
632     if (subdirp == NULL) {
633       FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
634       continue;
635     }
636 
637     // Since we don't create the backing store files in directories
638     // pointed to by symbolic links, we also don't follow them when
639     // looking for the files. We check for a symbolic link after the
640     // call to opendir in order to eliminate a small window where the
641     // symlink can be exploited.
642     //
643     if (!is_directory_secure(usrdir_name)) {
644       FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
645       os::closedir(subdirp);
646       continue;
647     }
648 
649     struct dirent* udentry;
650     errno = 0;
651     while ((udentry = os::readdir(subdirp)) != NULL) {
652 
653       if (filename_to_pid(udentry->d_name) == vmid) {
654         struct stat statbuf;
655         int result;
656 
657         char* filename = NEW_C_HEAP_ARRAY(char,
658                             strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
659 
660         strcpy(filename, usrdir_name);
661         strcat(filename, "/");
662         strcat(filename, udentry->d_name);
663 
664         // don't follow symbolic links for the file
665         RESTARTABLE(::lstat(filename, &statbuf), result);
666         if (result == OS_ERR) {
667            FREE_C_HEAP_ARRAY(char, filename, mtInternal);
668            continue;
669         }
670 
671         // skip over files that are not regular files.
672         if (!S_ISREG(statbuf.st_mode)) {
673           FREE_C_HEAP_ARRAY(char, filename, mtInternal);
674           continue;
675         }
676 
677         // compare and save filename with latest creation time
678         if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
679 
680           if (statbuf.st_ctime > oldest_ctime) {
681             char* user = strchr(dentry->d_name, '_') + 1;
682 
683             if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal);
684             oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
685 
686             strcpy(oldest_user, user);
687             oldest_ctime = statbuf.st_ctime;
688           }
689         }
690 
691         FREE_C_HEAP_ARRAY(char, filename, mtInternal);
692       }
693     }
694     os::closedir(subdirp);
695     FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
696   }
697   os::closedir(tmpdirp);
698 
699   return(oldest_user);
700 }
701 
702 // return the name of the user that owns the JVM indicated by the given vmid.
703 //
get_user_name(int vmid,TRAPS)704 static char* get_user_name(int vmid, TRAPS) {
705   return get_user_name_slow(vmid, THREAD);
706 }
707 
708 // return the file name of the backing store file for the named
709 // shared memory region for the given user name and vmid.
710 //
711 // the caller is expected to free the allocated memory.
712 //
get_sharedmem_filename(const char * dirname,int vmid)713 static char* get_sharedmem_filename(const char* dirname, int vmid) {
714 
715   // add 2 for the file separator and a null terminator.
716   size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
717 
718   char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
719   snprintf(name, nbytes, "%s/%d", dirname, vmid);
720 
721   return name;
722 }
723 
724 
725 // remove file
726 //
727 // this method removes the file specified by the given path
728 //
remove_file(const char * path)729 static void remove_file(const char* path) {
730 
731   int result;
732 
733   // if the file is a directory, the following unlink will fail. since
734   // we don't expect to find directories in the user temp directory, we
735   // won't try to handle this situation. even if accidentially or
736   // maliciously planted, the directory's presence won't hurt anything.
737   //
738   RESTARTABLE(::unlink(path), result);
739   if (PrintMiscellaneous && Verbose && result == OS_ERR) {
740     if (errno != ENOENT) {
741       warning("Could not unlink shared memory backing"
742               " store file %s : %s\n", path, strerror(errno));
743     }
744   }
745 }
746 
747 // Cleanup stale shared memory resources
748 //
749 // This method attempts to remove all stale shared memory files in
750 // the named user temporary directory. It scans the named directory
751 // for files matching the pattern ^$[0-9]*$. For each file found, the
752 // process id is extracted from the file name and a test is run to
753 // determine if the process is alive. If the process is not alive,
754 // any stale file resources are removed.
cleanup_sharedmem_resources(const char * dirname)755 static void cleanup_sharedmem_resources(const char* dirname) {
756 
757   int saved_cwd_fd;
758   // Open the directory.
759   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
760   if (dirp == NULL) {
761      // Directory doesn't exist or is insecure, so there is nothing to cleanup.
762     return;
763   }
764 
765   // For each entry in the directory that matches the expected file
766   // name pattern, determine if the file resources are stale and if
767   // so, remove the file resources. Note, instrumented HotSpot processes
768   // for this user may start and/or terminate during this search and
769   // remove or create new files in this directory. The behavior of this
770   // loop under these conditions is dependent upon the implementation of
771   // opendir/readdir.
772   struct dirent* entry;
773   errno = 0;
774   while ((entry = os::readdir(dirp)) != NULL) {
775 
776     pid_t pid = filename_to_pid(entry->d_name);
777 
778     if (pid == 0) {
779 
780       if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
781 
782         // Attempt to remove all unexpected files, except "." and "..".
783         unlink(entry->d_name);
784       }
785 
786       errno = 0;
787       continue;
788     }
789 
790     // We now have a file name that converts to a valid integer
791     // that could represent a process id . if this process id
792     // matches the current process id or the process is not running,
793     // then remove the stale file resources.
794     //
795     // Process liveness is detected by sending signal number 0 to
796     // the process id (see kill(2)). if kill determines that the
797     // process does not exist, then the file resources are removed.
798     // if kill determines that that we don't have permission to
799     // signal the process, then the file resources are assumed to
800     // be stale and are removed because the resources for such a
801     // process should be in a different user specific directory.
802     if ((pid == os::current_process_id()) ||
803         (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
804 
805         unlink(entry->d_name);
806     }
807     errno = 0;
808   }
809 
810   // Close the directory and reset the current working directory.
811   close_directory_secure_cwd(dirp, saved_cwd_fd);
812 
813 }
814 
815 // Make the user specific temporary directory. Returns true if
816 // the directory exists and is secure upon return. Returns false
817 // if the directory exists but is either a symlink, is otherwise
818 // insecure, or if an error occurred.
make_user_tmp_dir(const char * dirname)819 static bool make_user_tmp_dir(const char* dirname) {
820 
821   // Create the directory with 0755 permissions. note that the directory
822   // will be owned by euid::egid, which may not be the same as uid::gid.
823   if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
824     if (errno == EEXIST) {
825       // The directory already exists and was probably created by another
826       // JVM instance. However, this could also be the result of a
827       // deliberate symlink. Verify that the existing directory is safe.
828       if (!is_directory_secure(dirname)) {
829         // Directory is not secure.
830         if (PrintMiscellaneous && Verbose) {
831           warning("%s directory is insecure\n", dirname);
832         }
833         return false;
834       }
835     }
836     else {
837       // we encountered some other failure while attempting
838       // to create the directory
839       //
840       if (PrintMiscellaneous && Verbose) {
841         warning("could not create directory %s: %s\n",
842                 dirname, strerror(errno));
843       }
844       return false;
845     }
846   }
847   return true;
848 }
849 
850 // create the shared memory file resources
851 //
852 // This method creates the shared memory file with the given size
853 // This method also creates the user specific temporary directory, if
854 // it does not yet exist.
855 //
create_sharedmem_resources(const char * dirname,const char * filename,size_t size)856 static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) {
857 
858   // make the user temporary directory
859   if (!make_user_tmp_dir(dirname)) {
860     // could not make/find the directory or the found directory
861     // was not secure
862     return -1;
863   }
864 
865   int saved_cwd_fd;
866   // Open the directory and set the current working directory to it.
867   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
868   if (dirp == NULL) {
869     // Directory doesn't exist or is insecure, so cannot create shared
870     // memory file.
871     return -1;
872   }
873 
874   // Open the filename in the current directory.
875   // Cannot use O_TRUNC here; truncation of an existing file has to happen
876   // after the is_file_secure() check below.
877   int result;
878 
879   // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
880   // so provide a workaround in this case.
881 #ifdef O_NOFOLLOW
882   RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
883 #else
884   // workaround function (jdk6 code)
885   RESTARTABLE(::open_o_nofollow(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE), result);
886 #endif
887 
888   if (result == OS_ERR) {
889     if (PrintMiscellaneous && Verbose) {
890       if (errno == ELOOP) {
891         warning("file %s is a symlink and is not secure\n", filename);
892       } else {
893         warning("could not create file %s: %s\n", filename, strerror(errno));
894       }
895     }
896     // Close the directory and reset the current working directory.
897     close_directory_secure_cwd(dirp, saved_cwd_fd);
898 
899     return -1;
900   }
901   // Close the directory and reset the current working directory.
902   close_directory_secure_cwd(dirp, saved_cwd_fd);
903 
904   // save the file descriptor
905   int fd = result;
906 
907   // Check to see if the file is secure.
908   if (!is_file_secure(fd, filename)) {
909     ::close(fd);
910     return -1;
911   }
912 
913   // Truncate the file to get rid of any existing data.
914   RESTARTABLE(::ftruncate(fd, (off_t)0), result);
915   if (result == OS_ERR) {
916     if (PrintMiscellaneous && Verbose) {
917       warning("could not truncate shared memory file: %s\n", strerror(errno));
918     }
919     ::close(fd);
920     return -1;
921   }
922   // set the file size
923   RESTARTABLE(::ftruncate(fd, (off_t)size), result);
924   if (result == OS_ERR) {
925     if (PrintMiscellaneous && Verbose) {
926       warning("could not set shared memory file size: %s\n", strerror(errno));
927     }
928     RESTARTABLE(::close(fd), result);
929     return -1;
930   }
931 
932   return fd;
933 }
934 
935 // open the shared memory file for the given user and vmid. returns
936 // the file descriptor for the open file or -1 if the file could not
937 // be opened.
938 //
open_sharedmem_file(const char * filename,int oflags,TRAPS)939 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
940 
941   // open the file
942   int result;
943   // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
944   // so provide a workaround in this case
945 #ifdef O_NOFOLLOW
946   RESTARTABLE(::open(filename, oflags), result);
947 #else
948   RESTARTABLE(::open_o_nofollow(filename, oflags), result);
949 #endif
950 
951   if (result == OS_ERR) {
952     if (errno == ENOENT) {
953       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
954                   "Process not found");
955     }
956     else if (errno == EACCES) {
957       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
958                   "Permission denied");
959     }
960     else {
961       THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
962     }
963   }
964   int fd = result;
965 
966   // Check to see if the file is secure.
967   if (!is_file_secure(fd, filename)) {
968     ::close(fd);
969     return -1;
970   }
971 
972   return fd;
973 }
974 
975 // create a named shared memory region. returns the address of the
976 // memory region on success or NULL on failure. A return value of
977 // NULL will ultimately disable the shared memory feature.
978 //
979 // On Solaris and Linux, the name space for shared memory objects
980 // is the file system name space.
981 //
982 // A monitoring application attaching to a JVM does not need to know
983 // the file system name of the shared memory object. However, it may
984 // be convenient for applications to discover the existence of newly
985 // created and terminating JVMs by watching the file system name space
986 // for files being created or removed.
987 //
mmap_create_shared(size_t size)988 static char* mmap_create_shared(size_t size) {
989 
990   int result;
991   int fd;
992   char* mapAddress;
993 
994   int vmid = os::current_process_id();
995 
996   char* user_name = get_user_name(geteuid());
997 
998   if (user_name == NULL)
999     return NULL;
1000 
1001   char* dirname = get_user_tmp_dir(user_name);
1002   char* filename = get_sharedmem_filename(dirname, vmid);
1003 
1004   // Get the short filename.
1005   char* short_filename = strrchr(filename, '/');
1006   if (short_filename == NULL) {
1007     short_filename = filename;
1008   } else {
1009     short_filename++;
1010   }
1011 
1012   // cleanup any stale shared memory files
1013   cleanup_sharedmem_resources(dirname);
1014 
1015   assert(((size > 0) && (size % os::vm_page_size() == 0)),
1016          "unexpected PerfMemory region size");
1017 
1018   fd = create_sharedmem_resources(dirname, short_filename, size);
1019 
1020   FREE_C_HEAP_ARRAY(char, user_name, mtInternal);
1021   FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
1022 
1023   if (fd == -1) {
1024     FREE_C_HEAP_ARRAY(char, filename, mtInternal);
1025     return NULL;
1026   }
1027 
1028   mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1029 
1030   // attempt to close the file - restart it if it was interrupted,
1031   // but ignore other failures
1032   RESTARTABLE(::close(fd), result);
1033   assert(result != OS_ERR, "could not close file");
1034 
1035   if (mapAddress == MAP_FAILED) {
1036     if (PrintMiscellaneous && Verbose) {
1037       warning("mmap failed -  %s\n", strerror(errno));
1038     }
1039     remove_file(filename);
1040     FREE_C_HEAP_ARRAY(char, filename, mtInternal);
1041     return NULL;
1042   }
1043 
1044   // save the file name for use in delete_shared_memory()
1045   backing_store_file_name = filename;
1046 
1047   // clear the shared memory region
1048   (void)::memset((void*) mapAddress, 0, size);
1049 
1050   // It does not go through os api, the operation has to record from here.
1051   MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1052 
1053   return mapAddress;
1054 }
1055 
1056 // release a named shared memory region
1057 //
unmap_shared(char * addr,size_t bytes)1058 static void unmap_shared(char* addr, size_t bytes) {
1059   // Do not rely on os::reserve_memory/os::release_memory to use mmap.
1060   // Use os::reserve_memory/os::release_memory for PerfDisableSharedMem=1, mmap/munmap for PerfDisableSharedMem=0
1061   if (::munmap(addr, bytes) == -1) {
1062     warning("perfmemory: munmap failed (%d)\n", errno);
1063   }
1064 }
1065 
1066 // create the PerfData memory region in shared memory.
1067 //
create_shared_memory(size_t size)1068 static char* create_shared_memory(size_t size) {
1069 
1070   // create the shared memory region.
1071   return mmap_create_shared(size);
1072 }
1073 
1074 // delete the shared PerfData memory region
1075 //
delete_shared_memory(char * addr,size_t size)1076 static void delete_shared_memory(char* addr, size_t size) {
1077 
1078   // cleanup the persistent shared memory resources. since DestroyJavaVM does
1079   // not support unloading of the JVM, unmapping of the memory resource is
1080   // not performed. The memory will be reclaimed by the OS upon termination of
1081   // the process. The backing store file is deleted from the file system.
1082 
1083   assert(!PerfDisableSharedMem, "shouldn't be here");
1084 
1085   if (backing_store_file_name != NULL) {
1086     remove_file(backing_store_file_name);
1087     // Don't.. Free heap memory could deadlock os::abort() if it is called
1088     // from signal handler. OS will reclaim the heap memory.
1089     // FREE_C_HEAP_ARRAY(char, backing_store_file_name, mtInternal);
1090     backing_store_file_name = NULL;
1091   }
1092 }
1093 
1094 // return the size of the file for the given file descriptor
1095 // or 0 if it is not a valid size for a shared memory file
1096 //
sharedmem_filesize(int fd,TRAPS)1097 static size_t sharedmem_filesize(int fd, TRAPS) {
1098 
1099   struct stat statbuf;
1100   int result;
1101 
1102   RESTARTABLE(::fstat(fd, &statbuf), result);
1103   if (result == OS_ERR) {
1104     if (PrintMiscellaneous && Verbose) {
1105       warning("fstat failed: %s\n", strerror(errno));
1106     }
1107     THROW_MSG_0(vmSymbols::java_io_IOException(),
1108                 "Could not determine PerfMemory size");
1109   }
1110 
1111   if ((statbuf.st_size == 0) ||
1112      ((size_t)statbuf.st_size % os::vm_page_size() != 0)) {
1113     THROW_MSG_0(vmSymbols::java_lang_Exception(),
1114                 "Invalid PerfMemory size");
1115   }
1116 
1117   return (size_t)statbuf.st_size;
1118 }
1119 
1120 // attach to a named shared memory region.
1121 //
mmap_attach_shared(const char * user,int vmid,PerfMemory::PerfMemoryMode mode,char ** addr,size_t * sizep,TRAPS)1122 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
1123 
1124   char* mapAddress;
1125   int result;
1126   int fd;
1127   size_t size = 0;
1128   const char* luser = NULL;
1129 
1130   int mmap_prot;
1131   int file_flags;
1132 
1133   ResourceMark rm;
1134 
1135   // map the high level access mode to the appropriate permission
1136   // constructs for the file and the shared memory mapping.
1137   if (mode == PerfMemory::PERF_MODE_RO) {
1138     mmap_prot = PROT_READ;
1139 
1140   // No O_NOFOLLOW defined at buildtime, and it is not documented for open.
1141 #ifdef O_NOFOLLOW
1142     file_flags = O_RDONLY | O_NOFOLLOW;
1143 #else
1144     file_flags = O_RDONLY;
1145 #endif
1146   }
1147   else if (mode == PerfMemory::PERF_MODE_RW) {
1148 #ifdef LATER
1149     mmap_prot = PROT_READ | PROT_WRITE;
1150     file_flags = O_RDWR | O_NOFOLLOW;
1151 #else
1152     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1153               "Unsupported access mode");
1154 #endif
1155   }
1156   else {
1157     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1158               "Illegal access mode");
1159   }
1160 
1161   if (user == NULL || strlen(user) == 0) {
1162     luser = get_user_name(vmid, CHECK);
1163   }
1164   else {
1165     luser = user;
1166   }
1167 
1168   if (luser == NULL) {
1169     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1170               "Could not map vmid to user Name");
1171   }
1172 
1173   char* dirname = get_user_tmp_dir(luser);
1174 
1175   // since we don't follow symbolic links when creating the backing
1176   // store file, we don't follow them when attaching either.
1177   //
1178   if (!is_directory_secure(dirname)) {
1179     FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
1180     if (luser != user) {
1181       FREE_C_HEAP_ARRAY(char, luser, mtInternal);
1182     }
1183     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1184               "Process not found");
1185   }
1186 
1187   char* filename = get_sharedmem_filename(dirname, vmid);
1188 
1189   // copy heap memory to resource memory. the open_sharedmem_file
1190   // method below need to use the filename, but could throw an
1191   // exception. using a resource array prevents the leak that
1192   // would otherwise occur.
1193   char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
1194   strcpy(rfilename, filename);
1195 
1196   // free the c heap resources that are no longer needed
1197   if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal);
1198   FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
1199   FREE_C_HEAP_ARRAY(char, filename, mtInternal);
1200 
1201   // open the shared memory file for the give vmid
1202   fd = open_sharedmem_file(rfilename, file_flags, CHECK);
1203   assert(fd != OS_ERR, "unexpected value");
1204 
1205   if (*sizep == 0) {
1206     size = sharedmem_filesize(fd, CHECK);
1207     assert(size != 0, "unexpected size");
1208   } else {
1209     size = *sizep;
1210   }
1211 
1212   mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
1213 
1214   // attempt to close the file - restart if it gets interrupted,
1215   // but ignore other failures
1216   RESTARTABLE(::close(fd), result);
1217   assert(result != OS_ERR, "could not close file");
1218 
1219   if (mapAddress == MAP_FAILED) {
1220     if (PrintMiscellaneous && Verbose) {
1221       warning("mmap failed: %s\n", strerror(errno));
1222     }
1223     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
1224               "Could not map PerfMemory");
1225   }
1226 
1227   // It does not go through os api, the operation has to record from here.
1228   MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1229 
1230   *addr = mapAddress;
1231   *sizep = size;
1232 
1233   if (PerfTraceMemOps) {
1234     tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
1235                INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress);
1236   }
1237 }
1238 
1239 
1240 
1241 
1242 // create the PerfData memory region
1243 //
1244 // This method creates the memory region used to store performance
1245 // data for the JVM. The memory may be created in standard or
1246 // shared memory.
1247 //
create_memory_region(size_t size)1248 void PerfMemory::create_memory_region(size_t size) {
1249 
1250   if (PerfDisableSharedMem) {
1251     // do not share the memory for the performance data.
1252     _start = create_standard_memory(size);
1253   }
1254   else {
1255     _start = create_shared_memory(size);
1256     if (_start == NULL) {
1257 
1258       // creation of the shared memory region failed, attempt
1259       // to create a contiguous, non-shared memory region instead.
1260       //
1261       if (PrintMiscellaneous && Verbose) {
1262         warning("Reverting to non-shared PerfMemory region.\n");
1263       }
1264       PerfDisableSharedMem = true;
1265       _start = create_standard_memory(size);
1266     }
1267   }
1268 
1269   if (_start != NULL) _capacity = size;
1270 
1271 }
1272 
1273 // delete the PerfData memory region
1274 //
1275 // This method deletes the memory region used to store performance
1276 // data for the JVM. The memory region indicated by the <address, size>
1277 // tuple will be inaccessible after a call to this method.
1278 //
delete_memory_region()1279 void PerfMemory::delete_memory_region() {
1280 
1281   assert((start() != NULL && capacity() > 0), "verify proper state");
1282 
1283   // If user specifies PerfDataSaveFile, it will save the performance data
1284   // to the specified file name no matter whether PerfDataSaveToFile is specified
1285   // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
1286   // -XX:+PerfDataSaveToFile.
1287   if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
1288     save_memory_to_file(start(), capacity());
1289   }
1290 
1291   if (PerfDisableSharedMem) {
1292     delete_standard_memory(start(), capacity());
1293   }
1294   else {
1295     delete_shared_memory(start(), capacity());
1296   }
1297 }
1298 
1299 // attach to the PerfData memory region for another JVM
1300 //
1301 // This method returns an <address, size> tuple that points to
1302 // a memory buffer that is kept reasonably synchronized with
1303 // the PerfData memory region for the indicated JVM. This
1304 // buffer may be kept in synchronization via shared memory
1305 // or some other mechanism that keeps the buffer updated.
1306 //
1307 // If the JVM chooses not to support the attachability feature,
1308 // this method should throw an UnsupportedOperation exception.
1309 //
1310 // This implementation utilizes named shared memory to map
1311 // the indicated process's PerfData memory region into this JVMs
1312 // address space.
1313 //
attach(const char * user,int vmid,PerfMemoryMode mode,char ** addrp,size_t * sizep,TRAPS)1314 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
1315 
1316   if (vmid == 0 || vmid == os::current_process_id()) {
1317      *addrp = start();
1318      *sizep = capacity();
1319      return;
1320   }
1321 
1322   mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
1323 }
1324 
1325 // detach from the PerfData memory region of another JVM
1326 //
1327 // This method detaches the PerfData memory region of another
1328 // JVM, specified as an <address, size> tuple of a buffer
1329 // in this process's address space. This method may perform
1330 // arbitrary actions to accomplish the detachment. The memory
1331 // region specified by <address, size> will be inaccessible after
1332 // a call to this method.
1333 //
1334 // If the JVM chooses not to support the attachability feature,
1335 // this method should throw an UnsupportedOperation exception.
1336 //
1337 // This implementation utilizes named shared memory to detach
1338 // the indicated process's PerfData memory region from this
1339 // process's address space.
1340 //
detach(char * addr,size_t bytes,TRAPS)1341 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
1342 
1343   assert(addr != 0, "address sanity check");
1344   assert(bytes > 0, "capacity sanity check");
1345 
1346   if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
1347     // prevent accidental detachment of this process's PerfMemory region
1348     return;
1349   }
1350 
1351   unmap_shared(addr, bytes);
1352 }
1353 
backing_store_filename()1354 char* PerfMemory::backing_store_filename() {
1355   return backing_store_file_name;
1356 }
1357