mirror of
https://gitlab.com/apparmor/apparmor.git
synced 2025-03-04 00:14:44 +01:00
unix_fd_common tests: refactor out unix_fd_client code into common
The code from unix_fd_client can be used by other clients which will receive the fd of a file using SCM rights. Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com> MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/810 Acked-by: John Johansen <john.johansen@canonical.com>
This commit is contained in:
parent
a19ff53674
commit
b07a532a6f
5 changed files with 112 additions and 70 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -9,74 +9,9 @@
|
|||
* License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <alloca.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#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]));
|
||||
}
|
||||
|
|
85
tests/regression/apparmor/unix_fd_common.c
Normal file
85
tests/regression/apparmor/unix_fd_common.c
Normal file
|
@ -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 <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <alloca.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
17
tests/regression/apparmor/unix_fd_common.h
Normal file
17
tests/regression/apparmor/unix_fd_common.h
Normal file
|
@ -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);
|
Loading…
Add table
Reference in a new issue