1 /*
2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
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) {
|
1 /*
2 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
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 and
381 // handle errors, otherwise shared memory files will be created in cwd.
382 result = fchdir(fd);
383 if (result == OS_ERR) {
384 if (PrintMiscellaneous && Verbose) {
385 warning("could not change to directory %s", dirname);
386 }
387 if (*saved_cwd_fd != -1) {
388 ::close(*saved_cwd_fd);
389 *saved_cwd_fd = -1;
390 }
391 // Close the directory.
392 os::closedir(dirp);
393 return NULL;
394 } else {
395 return dirp;
396 }
397 }
398
399 // Close the directory and restore the current working directory.
400 //
401 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
402
403 int result;
404 // If we have a saved cwd change back to it and close the fd.
405 if (saved_cwd_fd != -1) {
406 result = fchdir(saved_cwd_fd);
407 ::close(saved_cwd_fd);
408 }
409
410 // Close the directory.
411 os::closedir(dirp);
412 }
413
414 // Check if the given file descriptor is considered a secure.
415 //
416 static bool is_file_secure(int fd, const char *filename) {
|