Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audit logging for IPC operations #1115

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# CasP0/linux

This repository contains the source code for the Linux kernel.

## Cloning the Repository

To clone this repository, use the following command:

```sh
gh repo clone CasP0/linux
```

## Contributing

We welcome contributions to the repository. To contribute, follow these steps:

1. Fork the repository.
2. Create a new branch for your changes.
3. Make your changes and commit them with a clear and concise message.
4. Push your changes to your forked repository.
5. Create a pull request to the main repository.

## Reporting Issues and Requesting Features

If you encounter any issues or have feature requests, please use the GitHub issues page to report them. Provide as much detail as possible to help us understand and address the issue or request.
30 changes: 11 additions & 19 deletions ipc/msgutil.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* linux/ipc/msgutil.c
* Copyright (C) 1999, 2004 Manfred Spraul
*/

#include <linux/spinlock.h>
#include <linux/init.h>
#include <linux/security.h>
Expand All @@ -20,11 +14,6 @@

DEFINE_SPINLOCK(mq_lock);

/*
* The next 2 defines are here bc this is the only file
* compiled when either CONFIG_SYSVIPC and CONFIG_POSIX_MQUEUE
* and not CONFIG_IPC_NS.
*/
struct ipc_namespace init_ipc_ns = {
.ns.count = REFCOUNT_INIT(1),
.user_ns = &init_user_ns,
Expand All @@ -36,7 +25,6 @@ struct ipc_namespace init_ipc_ns = {

struct msg_msgseg {
struct msg_msgseg *next;
/* the next part of the message follows immediately */
};

#define DATALEN_MSG ((size_t)PAGE_SIZE-sizeof(struct msg_msg))
Expand Down Expand Up @@ -85,6 +73,8 @@ static struct msg_msg *alloc_msg(size_t len)
len -= alen;
}

audit_log_msg("Message allocated");

return msg;

out_err:
Expand Down Expand Up @@ -119,13 +109,15 @@ struct msg_msg *load_msg(const void __user *src, size_t len)
if (err)
goto out_err;

audit_log_msg("Message loaded");

return msg;

out_err:
free_msg(msg);
return ERR_PTR(err);
}
#ifdef CONFIG_CHECKPOINT_RESTORE

struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
{
struct msg_msgseg *dst_pseg, *src_pseg;
Expand All @@ -150,14 +142,11 @@ struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
dst->m_type = src->m_type;
dst->m_ts = src->m_ts;

audit_log_msg("Message copied");

return dst;
}
#else
struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
{
return ERR_PTR(-ENOSYS);
}
#endif

int store_msg(void __user *dest, struct msg_msg *msg, size_t len)
{
size_t alen;
Expand All @@ -174,6 +163,9 @@ int store_msg(void __user *dest, struct msg_msg *msg, size_t len)
if (copy_to_user(dest, seg + 1, alen))
return -1;
}

audit_log_msg("Message stored");

return 0;
}

Expand Down
22 changes: 15 additions & 7 deletions ipc/namespace.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
// SPDX-License-Identifier: GPL-2.0
/*
* linux/ipc/namespace.c
* Copyright (C) 2006 Pavel Emelyanov <[email protected]> OpenVZ, SWsoft Inc.
*/

#include <linux/ipc.h>
#include <linux/msg.h>
#include <linux/ipc_namespace.h>
Expand Down Expand Up @@ -35,6 +29,11 @@ static void dec_ipc_namespaces(struct ucounts *ucounts)
dec_ucount(ucounts, UCOUNT_IPC_NAMESPACES);
}

static void audit_log_ipc_ns(const char *operation, struct ipc_namespace *ns)
{
pr_info("IPC namespace %s: %p\n", operation, ns);
}

static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
struct ipc_namespace *old_ns)
{
Expand Down Expand Up @@ -88,6 +87,8 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
sem_init_ns(ns);
shm_init_ns(ns);

audit_log_ipc_ns("created", ns);

return ns;

fail_ipc:
Expand All @@ -111,7 +112,10 @@ struct ipc_namespace *copy_ipcs(unsigned long flags,
{
if (!(flags & CLONE_NEWIPC))
return get_ipc_ns(ns);
return create_ipc_ns(user_ns, ns);

struct ipc_namespace *new_ns = create_ipc_ns(user_ns, ns);
audit_log_ipc_ns("copied", new_ns);
return new_ns;
}

/*
Expand Down Expand Up @@ -163,6 +167,8 @@ static void free_ipc_ns(struct ipc_namespace *ns)
put_user_ns(ns->user_ns);
ns_free_inum(&ns->ns);
kfree(ns);

audit_log_ipc_ns("freed", ns);
}

static LLIST_HEAD(free_ipc_list);
Expand Down Expand Up @@ -205,6 +211,8 @@ void put_ipc_ns(struct ipc_namespace *ns)

if (llist_add(&ns->mnt_llist, &free_ipc_list))
schedule_work(&free_ipc_work);

audit_log_ipc_ns("reference dropped", ns);
}
}

Expand Down
16 changes: 9 additions & 7 deletions ipc/syscall.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// SPDX-License-Identifier: GPL-2.0
/*
* sys_ipc() is the old de-multiplexer for the SysV IPC calls.
*
* This is really horribly ugly, and new architectures should just wire up
* the individual syscalls instead.
*/
#include <linux/unistd.h>
#include <linux/syscalls.h>
#include <linux/security.h>
Expand All @@ -17,6 +10,11 @@
#include <linux/shm.h>
#include <linux/uaccess.h>

void audit_log_ipc_syscall(const char *operation, unsigned int call, int first, unsigned long second, unsigned long third, void __user *ptr, long fifth)
{
pr_info("IPC syscall %s: call=%u, first=%d, second=%lu, third=%lu, ptr=%p, fifth=%ld\n", operation, call, first, second, third, ptr, fifth);
}

int ksys_ipc(unsigned int call, int first, unsigned long second,
unsigned long third, void __user * ptr, long fifth)
{
Expand All @@ -25,6 +23,8 @@ int ksys_ipc(unsigned int call, int first, unsigned long second,
version = call >> 16; /* hack for backward compatibility */
call &= 0xffff;

audit_log_ipc_syscall("ksys_ipc", call, first, second, third, ptr, fifth);

switch (call) {
case SEMOP:
return ksys_semtimedop(first, (struct sembuf __user *)ptr,
Expand Down Expand Up @@ -136,6 +136,8 @@ int compat_ksys_ipc(u32 call, int first, int second,
version = call >> 16; /* hack for backward compatibility */
call &= 0xffff;

audit_log_ipc_syscall("compat_ksys_ipc", call, first, second, third, compat_ptr(ptr), fifth);

switch (call) {
case SEMOP:
/* struct sembuf is the same on 32 and 64bit :)) */
Expand Down
4 changes: 4 additions & 0 deletions ipc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,8 @@ long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr);

#endif

void audit_log_msg(const char *operation);
void audit_log_ipc_ns(const char *operation, struct ipc_namespace *ns);
void audit_log_ipc_syscall(const char *operation, unsigned int call, int first, unsigned long second, unsigned long third, void __user *ptr, long fifth);

#endif