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