comparison src/os/solaris/vm/perfMemory_solaris.cpp @ 20804:7848fc12602b

Merge with jdk8u40-b25
author Gilles Duboscq <gilles.m.duboscq@oracle.com>
date Tue, 07 Apr 2015 14:58:49 +0200
parents 52b4284cb496 42f27b59c550
children 12dcf5ba8b34
comparison
equal deleted inserted replaced
20184:84105dcdb05b 20804:7848fc12602b
197 // successful conversion, return the pid 197 // successful conversion, return the pid
198 return pid; 198 return pid;
199 } 199 }
200 200
201 201
202 // check if the given path is considered a secure directory for 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 //
207 static 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 // See if the uid of the directory matches the effective uid of the process.
223 //
224 if (statp->st_uid != geteuid()) {
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
203 // the backing store files. Returns true if the directory exists 234 // the backing store files. Returns true if the directory exists
204 // and is considered a secure location. Returns false if the path 235 // and is considered a secure location. Returns false if the path
205 // is a symbolic link or if an error occurred. 236 // is a symbolic link or if an error occurred.
206 // 237 //
207 static bool is_directory_secure(const char* path) { 238 static bool is_directory_secure(const char* path) {
211 RESTARTABLE(::lstat(path, &statbuf), result); 242 RESTARTABLE(::lstat(path, &statbuf), result);
212 if (result == OS_ERR) { 243 if (result == OS_ERR) {
213 return false; 244 return false;
214 } 245 }
215 246
216 // the path exists, now check it's mode 247 // The path exists, see if it is secure.
217 if (S_ISLNK(statbuf.st_mode) || !S_ISDIR(statbuf.st_mode)) { 248 return is_statbuf_secure(&statbuf);
218 // the path represents a link or some non-directory file type, 249 }
219 // which is not what we expected. declare it insecure. 250
220 // 251
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.
256 //
257 static bool is_dirfd_secure(int dir_fd) {
258 struct stat statbuf;
259 int result = 0;
260
261 RESTARTABLE(::fstat(dir_fd, &statbuf), result);
262 if (result == OS_ERR) {
221 return false; 263 return false;
222 } 264 }
223 else { 265
224 // we have an existing directory, check if the permissions are safe. 266 // The path exists, now check its mode.
225 // 267 return is_statbuf_secure(&statbuf);
226 if ((statbuf.st_mode & (S_IWGRP|S_IWOTH)) != 0) { 268 }
227 // the directory is open for writing and could be subjected 269
228 // to a symlnk attack. declare it insecure. 270
229 // 271 // Check to make sure fd1 and fd2 are referencing the same file system object.
230 return false; 272 //
231 } 273 static bool is_same_fsobject(int fd1, int fd2) {
274 struct stat statbuf1;
275 struct stat statbuf2;
276 int result = 0;
277
278 RESTARTABLE(::fstat(fd1, &statbuf1), result);
279 if (result == OS_ERR) {
280 return false;
281 }
282 RESTARTABLE(::fstat(fd2, &statbuf2), result);
283 if (result == OS_ERR) {
284 return false;
285 }
286
287 if ((statbuf1.st_ino == statbuf2.st_ino) &&
288 (statbuf1.st_dev == statbuf2.st_dev)) {
289 return true;
290 } else {
291 return false;
292 }
293 }
294
295
296 // Open the directory of the given path and validate it.
297 // Return a DIR * of the open directory.
298 //
299 static DIR *open_directory_secure(const char* dirname) {
300 // Open the directory using open() so that it can be verified
301 // to be secure by calling is_dirfd_secure(), opendir() and then check
302 // to see if they are the same file system object. This method does not
303 // introduce a window of opportunity for the directory to be attacked that
304 // calling opendir() and is_directory_secure() does.
305 int result;
306 DIR *dirp = NULL;
307 RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result);
308 if (result == OS_ERR) {
309 // Directory doesn't exist or is a symlink, so there is nothing to cleanup.
310 if (PrintMiscellaneous && Verbose) {
311 if (errno == ELOOP) {
312 warning("directory %s is a symlink and is not secure\n", dirname);
313 } else {
314 warning("could not open directory %s: %s\n", dirname, strerror(errno));
315 }
316 }
317 return dirp;
318 }
319 int fd = result;
320
321 // Determine if the open directory is secure.
322 if (!is_dirfd_secure(fd)) {
323 // The directory is not a secure directory.
324 os::close(fd);
325 return dirp;
326 }
327
328 // Open the directory.
329 dirp = ::opendir(dirname);
330 if (dirp == NULL) {
331 // The directory doesn't exist, close fd and return.
332 os::close(fd);
333 return dirp;
334 }
335
336 // Check to make sure fd and dirp are referencing the same file system object.
337 if (!is_same_fsobject(fd, dirp->dd_fd)) {
338 // The directory is not secure.
339 os::close(fd);
340 os::closedir(dirp);
341 dirp = NULL;
342 return dirp;
343 }
344
345 // Close initial open now that we know directory is secure
346 os::close(fd);
347
348 return dirp;
349 }
350
351 // NOTE: The code below uses fchdir(), open() and unlink() because
352 // fdopendir(), openat() and unlinkat() are not supported on all
353 // versions. Once the support for fdopendir(), openat() and unlinkat()
354 // is available on all supported versions the code can be changed
355 // to use these functions.
356
357 // Open the directory of the given path, validate it and set the
358 // current working directory to it.
359 // Return a DIR * of the open directory and the saved cwd fd.
360 //
361 static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) {
362
363 // Open the directory.
364 DIR* dirp = open_directory_secure(dirname);
365 if (dirp == NULL) {
366 // Directory doesn't exist or is insecure, so there is nothing to cleanup.
367 return dirp;
368 }
369 int fd = dirp->dd_fd;
370
371 // Open a fd to the cwd and save it off.
372 int result;
373 RESTARTABLE(::open(".", O_RDONLY), result);
374 if (result == OS_ERR) {
375 *saved_cwd_fd = -1;
376 } else {
377 *saved_cwd_fd = result;
378 }
379
380 // Set the current directory to dirname by using the fd of the directory.
381 result = fchdir(fd);
382
383 return dirp;
384 }
385
386 // Close the directory and restore the current working directory.
387 //
388 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
389
390 int result;
391 // If we have a saved cwd change back to it and close the fd.
392 if (saved_cwd_fd != -1) {
393 result = fchdir(saved_cwd_fd);
394 ::close(saved_cwd_fd);
395 }
396
397 // Close the directory.
398 os::closedir(dirp);
399 }
400
401 // Check if the given file descriptor is considered a secure.
402 //
403 static bool is_file_secure(int fd, const char *filename) {
404
405 int result;
406 struct stat statbuf;
407
408 // Determine if the file is secure.
409 RESTARTABLE(::fstat(fd, &statbuf), result);
410 if (result == OS_ERR) {
411 if (PrintMiscellaneous && Verbose) {
412 warning("fstat failed on %s: %s\n", filename, strerror(errno));
413 }
414 return false;
415 }
416 if (statbuf.st_nlink > 1) {
417 // A file with multiple links is not expected.
418 if (PrintMiscellaneous && Verbose) {
419 warning("file %s has multiple links\n", filename);
420 }
421 return false;
232 } 422 }
233 return true; 423 return true;
234 } 424 }
235
236 425
237 // return the user name for the given user id 426 // return the user name for the given user id
238 // 427 //
239 // the caller is expected to free the allocated memory. 428 // the caller is expected to free the allocated memory.
240 // 429 //
306 char* oldest_user = NULL; 495 char* oldest_user = NULL;
307 time_t oldest_ctime = 0; 496 time_t oldest_ctime = 0;
308 497
309 const char* tmpdirname = os::get_temp_directory(); 498 const char* tmpdirname = os::get_temp_directory();
310 499
500 // open the temp directory
311 DIR* tmpdirp = os::opendir(tmpdirname); 501 DIR* tmpdirp = os::opendir(tmpdirname);
312 502
313 if (tmpdirp == NULL) { 503 if (tmpdirp == NULL) {
504 // Cannot open the directory to get the user name, return.
314 return NULL; 505 return NULL;
315 } 506 }
316 507
317 // for each entry in the directory that matches the pattern hsperfdata_*, 508 // for each entry in the directory that matches the pattern hsperfdata_*,
318 // open the directory and check if the file for the given vmid exists. 509 // open the directory and check if the file for the given vmid exists.
333 strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal); 524 strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
334 strcpy(usrdir_name, tmpdirname); 525 strcpy(usrdir_name, tmpdirname);
335 strcat(usrdir_name, "/"); 526 strcat(usrdir_name, "/");
336 strcat(usrdir_name, dentry->d_name); 527 strcat(usrdir_name, dentry->d_name);
337 528
338 DIR* subdirp = os::opendir(usrdir_name); 529 // open the user directory
530 DIR* subdirp = open_directory_secure(usrdir_name);
339 531
340 if (subdirp == NULL) { 532 if (subdirp == NULL) {
341 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); 533 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
342 continue; 534 continue;
343 } 535 }
502 } 694 }
503 } 695 }
504 } 696 }
505 697
506 698
507 // remove file
508 //
509 // this method removes the file with the given file name in the
510 // named directory.
511 //
512 static void remove_file(const char* dirname, const char* filename) {
513
514 size_t nbytes = strlen(dirname) + strlen(filename) + 2;
515 char* path = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
516
517 strcpy(path, dirname);
518 strcat(path, "/");
519 strcat(path, filename);
520
521 remove_file(path);
522
523 FREE_C_HEAP_ARRAY(char, path, mtInternal);
524 }
525
526
527 // cleanup stale shared memory resources 699 // cleanup stale shared memory resources
528 // 700 //
529 // This method attempts to remove all stale shared memory files in 701 // This method attempts to remove all stale shared memory files in
530 // the named user temporary directory. It scans the named directory 702 // the named user temporary directory. It scans the named directory
531 // for files matching the pattern ^$[0-9]*$. For each file found, the 703 // for files matching the pattern ^$[0-9]*$. For each file found, the
533 // determine if the process is alive. If the process is not alive, 705 // determine if the process is alive. If the process is not alive,
534 // any stale file resources are removed. 706 // any stale file resources are removed.
535 // 707 //
536 static void cleanup_sharedmem_resources(const char* dirname) { 708 static void cleanup_sharedmem_resources(const char* dirname) {
537 709
538 // open the user temp directory 710 int saved_cwd_fd;
539 DIR* dirp = os::opendir(dirname); 711 // open the directory
540 712 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
541 if (dirp == NULL) { 713 if (dirp == NULL) {
542 // directory doesn't exist, so there is nothing to cleanup 714 // directory doesn't exist or is insecure, so there is nothing to cleanup
543 return;
544 }
545
546 if (!is_directory_secure(dirname)) {
547 // the directory is not a secure directory
548 return; 715 return;
549 } 716 }
550 717
551 // for each entry in the directory that matches the expected file 718 // for each entry in the directory that matches the expected file
552 // name pattern, determine if the file resources are stale and if 719 // name pattern, determine if the file resources are stale and if
556 // loop under these conditions is dependent upon the implementation of 723 // loop under these conditions is dependent upon the implementation of
557 // opendir/readdir. 724 // opendir/readdir.
558 // 725 //
559 struct dirent* entry; 726 struct dirent* entry;
560 char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal); 727 char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal);
728
561 errno = 0; 729 errno = 0;
562 while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) { 730 while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
563 731
564 pid_t pid = filename_to_pid(entry->d_name); 732 pid_t pid = filename_to_pid(entry->d_name);
565 733
566 if (pid == 0) { 734 if (pid == 0) {
567 735
568 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 736 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
569 737
570 // attempt to remove all unexpected files, except "." and ".." 738 // attempt to remove all unexpected files, except "." and ".."
571 remove_file(dirname, entry->d_name); 739 unlink(entry->d_name);
572 } 740 }
573 741
574 errno = 0; 742 errno = 0;
575 continue; 743 continue;
576 } 744 }
589 // process should be in a different user specific directory. 757 // process should be in a different user specific directory.
590 // 758 //
591 if ((pid == os::current_process_id()) || 759 if ((pid == os::current_process_id()) ||
592 (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) { 760 (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
593 761
594 remove_file(dirname, entry->d_name); 762 unlink(entry->d_name);
595 } 763 }
596 errno = 0; 764 errno = 0;
597 } 765 }
598 os::closedir(dirp); 766
767 // close the directory and reset the current working directory
768 close_directory_secure_cwd(dirp, saved_cwd_fd);
769
599 FREE_C_HEAP_ARRAY(char, dbuf, mtInternal); 770 FREE_C_HEAP_ARRAY(char, dbuf, mtInternal);
600 } 771 }
601 772
602 // make the user specific temporary directory. Returns true if 773 // make the user specific temporary directory. Returns true if
603 // the directory exists and is secure upon return. Returns false 774 // the directory exists and is secure upon return. Returns false
650 // could not make/find the directory or the found directory 821 // could not make/find the directory or the found directory
651 // was not secure 822 // was not secure
652 return -1; 823 return -1;
653 } 824 }
654 825
826 int saved_cwd_fd;
827 // open the directory and set the current working directory to it
828 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
829 if (dirp == NULL) {
830 // Directory doesn't exist or is insecure, so cannot create shared
831 // memory file.
832 return -1;
833 }
834
835 // Open the filename in the current directory.
836 // Cannot use O_TRUNC here; truncation of an existing file has to happen
837 // after the is_file_secure() check below.
655 int result; 838 int result;
656 839 RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
657 RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE), result);
658 if (result == OS_ERR) { 840 if (result == OS_ERR) {
659 if (PrintMiscellaneous && Verbose) { 841 if (PrintMiscellaneous && Verbose) {
660 warning("could not create file %s: %s\n", filename, strerror(errno)); 842 if (errno == ELOOP) {
661 } 843 warning("file %s is a symlink and is not secure\n", filename);
844 } else {
845 warning("could not create file %s: %s\n", filename, strerror(errno));
846 }
847 }
848 // close the directory and reset the current working directory
849 close_directory_secure_cwd(dirp, saved_cwd_fd);
850
662 return -1; 851 return -1;
663 } 852 }
853 // close the directory and reset the current working directory
854 close_directory_secure_cwd(dirp, saved_cwd_fd);
664 855
665 // save the file descriptor 856 // save the file descriptor
666 int fd = result; 857 int fd = result;
667 858
859 // check to see if the file is secure
860 if (!is_file_secure(fd, filename)) {
861 ::close(fd);
862 return -1;
863 }
864
865 // truncate the file to get rid of any existing data
866 RESTARTABLE(::ftruncate(fd, (off_t)0), result);
867 if (result == OS_ERR) {
868 if (PrintMiscellaneous && Verbose) {
869 warning("could not truncate shared memory file: %s\n", strerror(errno));
870 }
871 ::close(fd);
872 return -1;
873 }
668 // set the file size 874 // set the file size
669 RESTARTABLE(::ftruncate(fd, (off_t)size), result); 875 RESTARTABLE(::ftruncate(fd, (off_t)size), result);
670 if (result == OS_ERR) { 876 if (result == OS_ERR) {
671 if (PrintMiscellaneous && Verbose) { 877 if (PrintMiscellaneous && Verbose) {
672 warning("could not set shared memory file size: %s\n", strerror(errno)); 878 warning("could not set shared memory file size: %s\n", strerror(errno));
698 } 904 }
699 else { 905 else {
700 THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR); 906 THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR);
701 } 907 }
702 } 908 }
703 909 int fd = result;
704 return result; 910
911 // check to see if the file is secure
912 if (!is_file_secure(fd, filename)) {
913 ::close(fd);
914 return -1;
915 }
916
917 return fd;
705 } 918 }
706 919
707 // create a named shared memory region. returns the address of the 920 // create a named shared memory region. returns the address of the
708 // memory region on success or NULL on failure. A return value of 921 // memory region on success or NULL on failure. A return value of
709 // NULL will ultimately disable the shared memory feature. 922 // NULL will ultimately disable the shared memory feature.
731 return NULL; 944 return NULL;
732 945
733 char* dirname = get_user_tmp_dir(user_name); 946 char* dirname = get_user_tmp_dir(user_name);
734 char* filename = get_sharedmem_filename(dirname, vmid); 947 char* filename = get_sharedmem_filename(dirname, vmid);
735 948
949 // get the short filename
950 char* short_filename = strrchr(filename, '/');
951 if (short_filename == NULL) {
952 short_filename = filename;
953 } else {
954 short_filename++;
955 }
956
736 // cleanup any stale shared memory files 957 // cleanup any stale shared memory files
737 cleanup_sharedmem_resources(dirname); 958 cleanup_sharedmem_resources(dirname);
738 959
739 assert(((size > 0) && (size % os::vm_page_size() == 0)), 960 assert(((size > 0) && (size % os::vm_page_size() == 0)),
740 "unexpected PerfMemory region size"); 961 "unexpected PerfMemory region size");
741 962
742 fd = create_sharedmem_resources(dirname, filename, size); 963 fd = create_sharedmem_resources(dirname, short_filename, size);
743 964
744 FREE_C_HEAP_ARRAY(char, user_name, mtInternal); 965 FREE_C_HEAP_ARRAY(char, user_name, mtInternal);
745 FREE_C_HEAP_ARRAY(char, dirname, mtInternal); 966 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
746 967
747 if (fd == -1) { 968 if (fd == -1) {
768 989
769 // clear the shared memory region 990 // clear the shared memory region
770 (void)::memset((void*) mapAddress, 0, size); 991 (void)::memset((void*) mapAddress, 0, size);
771 992
772 // it does not go through os api, the operation has to record from here 993 // it does not go through os api, the operation has to record from here
773 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, mtInternal, CURRENT_PC); 994 MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress,
995 size, CURRENT_PC, mtInternal);
774 996
775 return mapAddress; 997 return mapAddress;
776 } 998 }
777 999
778 // release a named shared memory region 1000 // release a named shared memory region
852 1074
853 // map the high level access mode to the appropriate permission 1075 // map the high level access mode to the appropriate permission
854 // constructs for the file and the shared memory mapping. 1076 // constructs for the file and the shared memory mapping.
855 if (mode == PerfMemory::PERF_MODE_RO) { 1077 if (mode == PerfMemory::PERF_MODE_RO) {
856 mmap_prot = PROT_READ; 1078 mmap_prot = PROT_READ;
857 file_flags = O_RDONLY; 1079 file_flags = O_RDONLY | O_NOFOLLOW;
858 } 1080 }
859 else if (mode == PerfMemory::PERF_MODE_RW) { 1081 else if (mode == PerfMemory::PERF_MODE_RW) {
860 #ifdef LATER 1082 #ifdef LATER
861 mmap_prot = PROT_READ | PROT_WRITE; 1083 mmap_prot = PROT_READ | PROT_WRITE;
862 file_flags = O_RDWR; 1084 file_flags = O_RDWR | O_NOFOLLOW;
863 #else 1085 #else
864 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 1086 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
865 "Unsupported access mode"); 1087 "Unsupported access mode");
866 #endif 1088 #endif
867 } 1089 }
939 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), 1161 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
940 "Could not map PerfMemory"); 1162 "Could not map PerfMemory");
941 } 1163 }
942 1164
943 // it does not go through os api, the operation has to record from here 1165 // it does not go through os api, the operation has to record from here
944 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, mtInternal, CURRENT_PC); 1166 MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress,
1167 size, CURRENT_PC, mtInternal);
945 1168
946 *addr = mapAddress; 1169 *addr = mapAddress;
947 *sizep = size; 1170 *sizep = size;
948 1171
949 if (PerfTraceMemOps) { 1172 if (PerfTraceMemOps) {