1 /* 2 * Copyright (c) 2024 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29 #include <darwintest.h> 30 #include <sys/types.h> 31 #include <sys/stat.h> 32 #include <fcntl.h> 33 #include <util.h> 34 35 T_GLOBAL_META(T_META_TAG_VM_PREFERRED); 36 37 T_DECL( 38 lseek_not_allowed_on_pipe, 39 "Ensure that lseek() is disallowed on pipes" 40 ) { 41 // rdar://3837316: Per the POSIX spec, lseek is disallowed on pipes. 42 // 43 T_SETUPBEGIN; 44 45 // Given a pipe 46 int fildes[2] = {-1, -1}; 47 T_ASSERT_POSIX_SUCCESS(pipe(&fildes[0]), "setup: created a pipe"); 48 49 T_SETUPEND; 50 51 // When I try to seek on it 52 // Then it fails 53 // And errno is set appropriately 54 T_ASSERT_POSIX_FAILURE(lseek(fildes[0], 0, SEEK_CUR), ESPIPE, "lseek fails with appropriate errno"); 55 } 56 57 T_DECL( 58 lseek_not_allowed_on_fifo, 59 "Ensure that lseek() is disallowed on FIFOs" 60 ) { 61 // rdar://3837316: Per the POSIX spec, lseek is disallowed on FIFOs. 62 T_SETUPBEGIN; 63 64 // Given a FIFO 65 char path_template[] = "/tmp/fifoXXXXXX"; 66 T_ASSERT_POSIX_SUCCESS(mkstemp(path_template), "setup: created temp file"); 67 // (Remove the file so we can create a FIFO in its place) 68 // (There's a race here between create->delete->create again) 69 T_ASSERT_POSIX_ZERO(remove(path_template), "setup: removed temp file"); 70 T_ASSERT_POSIX_SUCCESS(mkfifo(path_template, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)), "setup: created FIFO in temp file's place"); 71 int fifo_fd = open(path_template, O_RDWR, 0); 72 T_ASSERT_POSIX_SUCCESS(fifo_fd, "setup: opened FIFO"); 73 74 T_SETUPEND; 75 76 // When I try to seek on it 77 // Then it fails 78 // And errno is set appropriately 79 T_ASSERT_POSIX_FAILURE(lseek(fifo_fd, 0, SEEK_CUR), ESPIPE, "lseek fails with appropriate errno"); 80 } 81 82 T_DECL( 83 lseek_not_allowed_on_tty, 84 "Ensure that lseek() is disallowed on TTY device files" 85 ) { 86 // rdar://120750171: Seeking a TTY is undefined and should be denied. 87 T_SETUPBEGIN; 88 89 // Given a TTY device 90 int primary_fd, replica_fd; 91 T_ASSERT_POSIX_SUCCESS(openpty(&primary_fd, &replica_fd, NULL, NULL, NULL), "setup: open PTY"); 92 93 T_SETUPEND; 94 95 // When I try to seek on it 96 // Then it fails 97 // And errno is set appropriately 98 T_ASSERT_POSIX_FAILURE(lseek(primary_fd, 0, SEEK_CUR), ESPIPE, "lseek fails with appropriate errno"); 99 } 100