diff --git a/.gitignore b/.gitignore index 783f98ab8..0af376bec 100644 --- a/.gitignore +++ b/.gitignore @@ -216,6 +216,7 @@ utils/vim/apparmor.vim utils/vim/apparmor.vim.5 utils/vim/apparmor.vim.5.html utils/vim/pod2htmd.tmp +tests/regression/apparmor/*.o tests/regression/apparmor/aa_policy_cache tests/regression/apparmor/access tests/regression/apparmor/at_secure @@ -233,7 +234,6 @@ tests/regression/apparmor/chgrp tests/regression/apparmor/chmod tests/regression/apparmor/chown tests/regression/apparmor/clone -tests/regression/apparmor/dbus_common.o tests/regression/apparmor/dbus_eavesdrop tests/regression/apparmor/dbus_message tests/regression/apparmor/dbus_service @@ -292,7 +292,6 @@ tests/regression/apparmor/unix_fd_client tests/regression/apparmor/unix_fd_server tests/regression/apparmor/unix_socket tests/regression/apparmor/unix_socket_client -tests/regression/apparmor/unix_socket_common.o tests/regression/apparmor/unlink tests/regression/apparmor/uservars.inc tests/regression/apparmor/xattrs diff --git a/tests/regression/apparmor/Makefile b/tests/regression/apparmor/Makefile index 5c275a3cd..bf17431a3 100644 --- a/tests/regression/apparmor/Makefile +++ b/tests/regression/apparmor/Makefile @@ -317,6 +317,12 @@ unix_socket_client: unix_socket_client.c unix_socket_common.o unix_socket: unix_socket.c unix_socket_common.o unix_socket_client ${CC} ${CFLAGS} ${LDFLAGS} $(filter-out unix_socket_client, $^) -o $@ ${LDLIBS} +unix_fd_common.o: unix_fd_common.c unix_fd_common.h + ${CC} ${CFLAGS} ${LDFLAGS} $< -c ${LDLIBS} + +unix_fd_client: unix_fd_client.c unix_fd_common.o + ${CC} ${CFLAGS} ${LDFLAGS} $^ -o $@ ${LDLIBS} + build-dep: @if [ `whoami` = "root" ] ;\ then \ @@ -377,6 +383,6 @@ alltests: all fi clean: - rm -f $(EXEC) dbus_common.o unix_socket_common.o uservars.inc + rm -f $(EXEC) dbus_common.o unix_socket_common.o uservars.inc unix_fd_common.o regex.sh: open exec diff --git a/tests/regression/apparmor/unix_fd_client.c b/tests/regression/apparmor/unix_fd_client.c index 77b284d1e..4b2e32798 100644 --- a/tests/regression/apparmor/unix_fd_client.c +++ b/tests/regression/apparmor/unix_fd_client.c @@ -9,74 +9,9 @@ * License. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include "unix_fd_common.h" int main(int argc, char *argv[]) { - int sock, fd, len; - struct sockaddr_un remote; - char read_buffer[17], f_buf[255]; - struct iovec vect; - struct msghdr mesg; - struct cmsghdr *ctrl_mesg; - - if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { - fprintf(stderr, "FAIL CLIENT - sock %s\n", - strerror(errno)); - exit(1); - } - - remote.sun_family = AF_UNIX; - strcpy(remote.sun_path, argv[1]); - len = strlen(remote.sun_path) + sizeof(remote.sun_family); - if (connect(sock, (struct sockaddr *)&remote, len) == -1) { - fprintf(stderr, "FAIL CLIENT - connect %s\n", - strerror(errno)); - exit(1); - } - - vect.iov_base = f_buf; - vect.iov_len = 255; - - mesg.msg_name = NULL; - mesg.msg_namelen=0; - mesg.msg_iov = &vect; - mesg.msg_iovlen = 1; - - ctrl_mesg = alloca(sizeof (struct cmsghdr) + sizeof(fd)); - ctrl_mesg->cmsg_len = sizeof(struct cmsghdr) + sizeof(fd); - mesg.msg_control = ctrl_mesg; - mesg.msg_controllen = ctrl_mesg->cmsg_len; - - if (!recvmsg(sock, &mesg,0 )) { - fprintf(stderr, "FAIL CLIENT - recvmsg\n"); - exit(1); - } - - /* get mr. file descriptor */ - - memcpy(&fd, CMSG_DATA(ctrl_mesg), sizeof(fd)); - - if (pread(fd, read_buffer, 16, 0) <= 0) { - /* Failure */ - fprintf(stderr, "FAIL CLIENT - could not read\n"); - send(sock, "FAILFAILFAILFAIL", 16, 0); - exit(1); - } else { - send(sock, read_buffer, strlen(read_buffer),0); - } - - /* looks like it worked */ - exit(0); + exit(get_unix_clientfd(argv[1])); } diff --git a/tests/regression/apparmor/unix_fd_common.c b/tests/regression/apparmor/unix_fd_common.c new file mode 100644 index 000000000..72405078d --- /dev/null +++ b/tests/regression/apparmor/unix_fd_common.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2021 Canonical, Ltd. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, contact Canonical Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int get_unix_clientfd(char *sun_path) { + int sock, fd, len; + struct sockaddr_un remote; + char read_buffer[17], f_buf[255]; + struct iovec vect; + struct msghdr mesg; + struct cmsghdr *ctrl_mesg; + + if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + fprintf(stderr, "FAIL CLIENT - sock %s\n", + strerror(errno)); + return -1; + } + + remote.sun_family = AF_UNIX; + strcpy(remote.sun_path, sun_path); + len = strlen(remote.sun_path) + sizeof(remote.sun_family); + if (connect(sock, (struct sockaddr *)&remote, len) == -1) { + fprintf(stderr, "FAIL CLIENT - connect %s\n", + strerror(errno)); + return -1; + } + + vect.iov_base = f_buf; + vect.iov_len = 255; + + mesg.msg_name = NULL; + mesg.msg_namelen=0; + mesg.msg_iov = &vect; + mesg.msg_iovlen = 1; + + ctrl_mesg = alloca(sizeof (struct cmsghdr) + sizeof(fd)); + ctrl_mesg->cmsg_len = sizeof(struct cmsghdr) + sizeof(fd); + mesg.msg_control = ctrl_mesg; + mesg.msg_controllen = ctrl_mesg->cmsg_len; + + if (!recvmsg(sock, &mesg,0 )) { + fprintf(stderr, "FAIL CLIENT - recvmsg\n"); + return -1; + } + + /* get mr. file descriptor */ + + memcpy(&fd, CMSG_DATA(ctrl_mesg), sizeof(fd)); + + if (pread(fd, read_buffer, 16, 0) <= 0) { + /* Failure */ + fprintf(stderr, "FAIL CLIENT - could not read\n"); + send(sock, "FAILFAILFAILFAIL", 16, 0); + return -1; + } else { + send(sock, read_buffer, strlen(read_buffer),0); + } + return 0; +} diff --git a/tests/regression/apparmor/unix_fd_common.h b/tests/regression/apparmor/unix_fd_common.h new file mode 100644 index 000000000..cbc802416 --- /dev/null +++ b/tests/regression/apparmor/unix_fd_common.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) 2021 Canonical, Ltd. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, contact Canonical Ltd. + */ + +int get_unix_clientfd(char *sun_path);