Make more fixes and improvements

- Remove PAGESIZE constant
- Fix realloc() documentation
- Fix ttyname_r() error reporting
- Make forking more reliable on Windows
- Make execvp() a few microseconds faster
- Make system() a few microseconds faster
- Tighten up the socket-related magic numbers
- Loosen restrictions on mmap() offset alignment
- Improve GetProgramExecutableName() with getenv("_")
- Use mkstemp() as basis for mktemp(), tmpfile(), tmpfd()
- Fix flakes in pthread_cancel_test, unix_test, fork_test
- Fix recently introduced futex stack overflow regression
- Let sockets be passed as stdio to subprocesses on Windows
- Improve security of bind() on Windows w/ SO_EXCLUSIVEADDRUSE
This commit is contained in:
Justine Tunney 2023-07-29 18:44:15 -07:00
parent 140a8a52e5
commit 18bb5888e1
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
311 changed files with 1239 additions and 2622 deletions

View file

@ -23,13 +23,14 @@
* Creates client socket file descriptor for incoming connection.
*
* @param fd is the server socket file descriptor
* @param out_addr will receive the remote address
* @param inout_addrsize provides and receives addr's byte length
* @param opt_out_addr will receive the remote address
* @param opt_inout_addrsize provides and receives addr's byte length
* @return client fd which needs close(), or -1 w/ errno
* @cancellationpoint
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
int accept(int fd, struct sockaddr *out_addr, uint32_t *inout_addrsize) {
return accept4(fd, out_addr, inout_addrsize, 0);
int accept(int fd, struct sockaddr *opt_out_addr,
uint32_t *opt_inout_addrsize) {
return accept4(fd, opt_out_addr, opt_inout_addrsize, 0);
}

View file

@ -32,8 +32,8 @@
* Creates client socket file descriptor for incoming connection.
*
* @param fd is the server socket file descriptor
* @param out_addr will receive the remote address
* @param inout_addrsize provides and receives out_addr's byte length
* @param opt_out_addr will receive the remote address
* @param opt_inout_addrsize provides and receives out_addr's byte length
* @param flags can have SOCK_{CLOEXEC,NONBLOCK}, which may apply to
* both the newly created socket and the server one
* @return client fd which needs close(), or -1 w/ errno
@ -41,7 +41,7 @@
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
int accept4(int fd, struct sockaddr *out_addr, uint32_t *inout_addrsize,
int accept4(int fd, struct sockaddr *opt_out_addr, uint32_t *opt_inout_addrsize,
int flags) {
int rc;
struct sockaddr_storage ss = {0};
@ -61,11 +61,13 @@ int accept4(int fd, struct sockaddr *out_addr, uint32_t *inout_addrsize,
if (IsBsd()) {
__convert_bsd_to_sockaddr(&ss);
}
__write_sockaddr(&ss, out_addr, inout_addrsize);
__write_sockaddr(&ss, opt_out_addr, opt_inout_addrsize);
}
END_CANCELLATION_POINT;
STRACE("accept4(%d, [%s]) -> %d% lm", fd,
DescribeSockaddr(out_addr, inout_addrsize ? *inout_addrsize : 0), rc);
DescribeSockaddr(opt_out_addr,
opt_inout_addrsize ? *opt_inout_addrsize : 0),
rc);
return rc;
}

View file

@ -17,15 +17,14 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/struct/fd.internal.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/syscall_fd.internal.h"
#include "libc/sock/yoink.inc"
#include "libc/sysv/errfuns.h"
textwindows int sys_bind_nt(struct Fd *fd, const void *addr,
uint32_t addrsize) {
npassert(fd->kind == kFdSocket);
unassert(fd->kind == kFdSocket);
if (__sys_bind_nt(fd->handle, addr, addrsize) != -1) {
return 0;
} else {

View file

@ -32,7 +32,11 @@
*
* struct sockaddr_in in = {AF_INET, htons(12345), {htonl(0x7f000001)}};
* int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
* bind(fd, &in, sizeof(in));
* bind(fd, (struct sockaddr *)&in, sizeof(in));
*
* On Windows, Cosmopolitan's implementation of bind() takes care of
* always setting the WIN32-specific `SO_EXCLUSIVEADDRUSE` option on
* inet stream sockets in order to safeguard your servers from tests
*
* @param fd is the file descriptor returned by socket()
* @param addr is usually the binary-encoded ip:port on which to listen

View file

@ -65,7 +65,7 @@ const char *(DescribeSockaddr)(char buf[128], const struct sockaddr *sa,
unix = (const struct sockaddr_un *)sa;
n = strnlen(unix->sun_path, sizeof(unix->sun_path));
n = MIN(n, 128 - 1);
memcpy(buf, unix->sun_path, n);
if (n) memcpy(buf, unix->sun_path, n);
buf[n] = 0;
}
}

View file

@ -16,14 +16,19 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/mem/mem.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/iphlpapi.h"
#include "libc/nt/winsock.h"
#include "libc/sock/internal.h"
#include "libc/sock/yoink.inc"
#include "libc/str/str.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/ipproto.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/so.h"
@ -42,13 +47,22 @@ __static_yoink("_dupsockfd");
textwindows int sys_socket_nt(int family, int type, int protocol) {
int64_t h;
struct SockFd *sockfd;
int fd, oflags, truetype;
int fd, oflags, truetype, yes = 1;
fd = __reservefd(-1);
if (fd == -1) return -1;
truetype = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
if ((h = WSASocket(family, truetype, protocol, NULL, 0,
kNtWsaFlagOverlapped)) != -1) {
oflags = 0;
// sets SO_EXCLUSIVEADDRUSE on all sockets so they won't get pilfered
// you can read a blog post on this subject in the find_unused_port()
// pydoc of this file third_party/python/Lib/test/support/__init__.py
// this needs to happen right after socket is called or it won't work
if (family == AF_INET || family == AF_INET6) {
unassert(__sys_setsockopt_nt(h, SOL_SOCKET, -5, &yes, 4) != -1);
}
oflags = O_RDWR;
if (type & SOCK_CLOEXEC) oflags |= O_CLOEXEC;
if (type & SOCK_NONBLOCK) oflags |= O_NONBLOCK;
sockfd = calloc(1, sizeof(struct SockFd));
@ -62,6 +76,7 @@ textwindows int sys_socket_nt(int family, int type, int protocol) {
g_fds.p[fd].handle = h;
g_fds.p[fd].extra = (uintptr_t)sockfd;
__fds_unlock();
return fd;
} else {
__releasefd(fd);

View file

@ -22,26 +22,31 @@
#include "libc/sock/internal.h"
#include "libc/sock/sock.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/errfuns.h"
/**
* Creates new system resource for network communication, e.g.
*
* int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
*
* @param family can be AF_UNIX, AF_INET, etc.
* @param family can be AF_UNIX, AF_INET, AF_INET6, etc.
* @param type can be SOCK_STREAM (for TCP), SOCK_DGRAM (e.g. UDP), or
* SOCK_RAW (IP) so long as IP_HDRINCL was passed to setsockopt();
* and additionally, may be or'd with SOCK_NONBLOCK, SOCK_CLOEXEC
* @param protocol can be IPPROTO_TCP, IPPROTO_UDP, or IPPROTO_ICMP
* @return socket file descriptor or -1 w/ errno
* @error ENETDOWN, EPFNOSUPPORT, etc.
* @raise EAFNOSUPPORT if `family` isn't supported by system or platform
* @see libc/sysv/consts.sh
* @asyncsignalsafe
*/
int socket(int family, int type, int protocol) {
int rc;
if (family == AF_UNSPEC) family = AF_INET;
if (!IsWindows()) {
if (family == AF_UNSPEC) {
family = AF_INET;
}
if (family == -1) {
rc = eafnosupport();
} else if (!IsWindows()) {
rc = sys_socket(family, type, protocol);
} else {
rc = sys_socket_nt(family, type, protocol);