blob: 0a159f69b3bbeb82440268a263c33be8d0dc71dd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/ipc/util.h
4 * Copyright (C) 1999 Christoph Rohland
5 *
Christian Kujau624dffc2006-01-15 02:43:54 +01006 * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Kirill Korotaev73ea4132006-10-02 02:18:20 -07007 * namespaces support. 2006 OpenVZ, SWsoft Inc.
8 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#ifndef _IPC_UTIL_H
12#define _IPC_UTIL_H
13
Johannes Weiner232086b2009-06-20 02:23:29 +020014#include <linux/unistd.h>
Nadia Derbey023a5352007-10-18 23:40:51 -070015#include <linux/err.h>
Davidlohr Bueso15df03c82017-11-17 15:31:18 -080016#include <linux/ipc_namespace.h>
Nadia Derbey7ca7e562007-10-18 23:40:48 -070017
Eric W. Biedermanf83a3962018-03-22 21:45:50 -050018#define IPCMNI 32768 /* <= MAX_INT limit for ipc arrays (including sysctl changes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define SEQ_MULTIPLIER (IPCMNI)
20
Davidlohr Buesoeae04d22018-08-21 22:01:56 -070021void sem_init(void);
22void msg_init(void);
Manfred Spraul239521f2014-01-27 17:07:04 -080023void shm_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080025struct ipc_namespace;
Eric W. Biederman03f1fc02018-03-23 00:22:05 -050026struct pid_namespace;
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080027
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070028#ifdef CONFIG_POSIX_MQUEUE
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -070029extern void mq_clear_sbinfo(struct ipc_namespace *ns);
30extern void mq_put_mnt(struct ipc_namespace *ns);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070031#else
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -070032static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
33static inline void mq_put_mnt(struct ipc_namespace *ns) { }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070034#endif
35
36#ifdef CONFIG_SYSVIPC
Davidlohr Buesoeae04d22018-08-21 22:01:56 -070037void sem_init_ns(struct ipc_namespace *ns);
38void msg_init_ns(struct ipc_namespace *ns);
39void shm_init_ns(struct ipc_namespace *ns);
Kirill Korotaev73ea4132006-10-02 02:18:20 -070040
41void sem_exit_ns(struct ipc_namespace *ns);
42void msg_exit_ns(struct ipc_namespace *ns);
43void shm_exit_ns(struct ipc_namespace *ns);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070044#else
Davidlohr Buesoeae04d22018-08-21 22:01:56 -070045static inline void sem_init_ns(struct ipc_namespace *ns) { }
46static inline void msg_init_ns(struct ipc_namespace *ns) { }
47static inline void shm_init_ns(struct ipc_namespace *ns) { }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070048
49static inline void sem_exit_ns(struct ipc_namespace *ns) { }
50static inline void msg_exit_ns(struct ipc_namespace *ns) { }
51static inline void shm_exit_ns(struct ipc_namespace *ns) { }
52#endif
Kirill Korotaev73ea4132006-10-02 02:18:20 -070053
Nadia Derbey7748dbf2007-10-18 23:40:49 -070054/*
55 * Structure that holds the parameters needed by the ipc operations
56 * (see after)
57 */
58struct ipc_params {
59 key_t key;
60 int flg;
61 union {
62 size_t size; /* for shared memories */
63 int nsems; /* for semaphores */
64 } u; /* holds the getnew() specific param */
65};
66
67/*
68 * Structure that holds some ipc operations. This structure is used to unify
69 * the calls to sys_msgget(), sys_semget(), sys_shmget()
70 * . routine to call to create a new ipc object. Can be one of newque,
71 * newary, newseg
Nadia Derbeyf4566f02007-10-18 23:40:53 -070072 * . routine to call to check permissions for a new ipc object.
Nadia Derbey7748dbf2007-10-18 23:40:49 -070073 * Can be one of security_msg_associate, security_sem_associate,
74 * security_shm_associate
75 * . routine to call for an extra check if needed
76 */
77struct ipc_ops {
Paul McQuade46c0a8c2014-06-06 14:37:37 -070078 int (*getnew)(struct ipc_namespace *, struct ipc_params *);
79 int (*associate)(struct kern_ipc_perm *, int);
80 int (*more_checks)(struct kern_ipc_perm *, struct ipc_params *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070081};
82
Mike Waychisonae781772005-09-06 15:17:09 -070083struct seq_file;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080084struct ipc_ids;
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070085
Davidlohr Buesoeae04d22018-08-21 22:01:56 -070086void ipc_init_ids(struct ipc_ids *ids);
Mike Waychisonae781772005-09-06 15:17:09 -070087#ifdef CONFIG_PROC_FS
88void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -070089 int ids, int (*show)(struct seq_file *, void *));
Eric W. Biederman03f1fc02018-03-23 00:22:05 -050090struct pid_namespace *ipc_seq_pid_ns(struct seq_file *);
Mike Waychisonae781772005-09-06 15:17:09 -070091#else
92#define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
93#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Kirill Korotaev73ea4132006-10-02 02:18:20 -070095#define IPC_SEM_IDS 0
96#define IPC_MSG_IDS 1
97#define IPC_SHM_IDS 2
98
Nadia Derbeyce621f52007-10-18 23:40:52 -070099#define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800100#define ipcid_to_seqx(id) ((id) / SEQ_MULTIPLIER)
Davidlohr Buesodaf948c2014-01-27 17:07:09 -0800101#define IPCID_SEQ_MAX min_t(int, INT_MAX/SEQ_MULTIPLIER, USHRT_MAX)
Nadia Derbeyce621f52007-10-18 23:40:52 -0700102
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700103/* must be called with ids->rwsem acquired for writing */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700104int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* must be called with both locks acquired. */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700107void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -0700109/* must be called with both locks acquired. */
110void ipc_set_key_private(struct ipc_ids *, struct kern_ipc_perm *);
111
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700112/* must be called with ipcp locked */
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700113int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Davidlohr Bueso15df03c82017-11-17 15:31:18 -0800115/**
Manfred Spraul27c331a2018-08-21 22:02:00 -0700116 * ipc_get_maxidx - get the highest assigned index
Davidlohr Bueso15df03c82017-11-17 15:31:18 -0800117 * @ids: ipc identifier set
118 *
119 * Called with ipc_ids.rwsem held for reading.
120 */
Manfred Spraul27c331a2018-08-21 22:02:00 -0700121static inline int ipc_get_maxidx(struct ipc_ids *ids)
Davidlohr Bueso15df03c82017-11-17 15:31:18 -0800122{
123 if (ids->in_use == 0)
124 return -1;
125
126 if (ids->in_use == IPCMNI)
127 return IPCMNI - 1;
128
Manfred Spraul27c331a2018-08-21 22:02:00 -0700129 return ids->max_idx;
Davidlohr Bueso15df03c82017-11-17 15:31:18 -0800130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * For allocation that need to be freed by RCU.
134 * Objects are reference counted, they start with reference count 1.
135 * getref increases the refcount, the putref call that reduces the recount
136 * to 0 schedules the rcu destruction. Caller must guarantee locking.
Manfred Spraul62b49c92017-07-12 14:35:34 -0700137 *
138 * refcount is initialized by ipc_addid(), before that point call_rcu()
139 * must be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Manfred Spraul2a9d6482018-08-21 22:02:04 -0700141bool ipc_rcu_getref(struct kern_ipc_perm *ptr);
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700142void ipc_rcu_putref(struct kern_ipc_perm *ptr,
143 void (*func)(struct rcu_head *head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700145struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
148void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
Eric W. Biederman1efdb692012-02-07 16:54:11 -0800149int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
Manfred Spraul4241c1a2018-08-21 22:01:34 -0700150struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns,
Davidlohr Bueso444d0f62013-04-30 19:15:24 -0700151 struct ipc_ids *ids, int id, int cmd,
152 struct ipc64_perm *perm, int extra_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Eric W. Biederman03f1fc02018-03-23 00:22:05 -0500154static inline void ipc_update_pid(struct pid **pos, struct pid *pid)
155{
156 struct pid *old = *pos;
157 if (old != pid) {
158 *pos = get_pid(pid);
159 put_pid(old);
160 }
161}
162
Will Deaconc1d7e012012-07-30 14:42:46 -0700163#ifndef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700164/* On IA-64, we always use the "64-bit version" of the IPC structures. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165# define ipc_parse_version(cmd) IPC_64
166#else
Manfred Spraul239521f2014-01-27 17:07:04 -0800167int ipc_parse_version(int *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#endif
169
170extern void free_msg(struct msg_msg *msg);
Mathias Krause4e9b45a2013-11-12 15:11:47 -0800171extern struct msg_msg *load_msg(const void __user *src, size_t len);
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800172extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
Mathias Krause4e9b45a2013-11-12 15:11:47 -0800173extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700174
Manfred Spraul27c331a2018-08-21 22:02:00 -0700175static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int id)
Nadia Derbey023a5352007-10-18 23:40:51 -0700176{
Manfred Spraul27c331a2018-08-21 22:02:00 -0700177 return ipcid_to_seqx(id) != ipcp->seq;
Nadia Derbey023a5352007-10-18 23:40:51 -0700178}
179
Davidlohr Bueso1ca70032013-07-08 16:01:10 -0700180static inline void ipc_lock_object(struct kern_ipc_perm *perm)
181{
182 spin_lock(&perm->lock);
183}
184
185static inline void ipc_unlock_object(struct kern_ipc_perm *perm)
186{
187 spin_unlock(&perm->lock);
188}
189
190static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm)
191{
192 assert_spin_locked(&perm->lock);
193}
194
Nadia Derbey023a5352007-10-18 23:40:51 -0700195static inline void ipc_unlock(struct kern_ipc_perm *perm)
196{
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700197 ipc_unlock_object(perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700198 rcu_read_unlock();
199}
200
Rafael Aquini0f3d2b02014-01-27 17:07:01 -0800201/*
202 * ipc_valid_object() - helper to sort out IPC_RMID races for codepaths
203 * where the respective ipc_ids.rwsem is not being held down.
204 * Checks whether the ipc object is still around or if it's gone already, as
205 * ipc_rmid() may have already freed the ID while the ipc lock was spinning.
206 * Needs to be called with kern_ipc_perm.lock held -- exception made for one
207 * checkpoint case at sys_semtimedop() as noted in code commentary.
208 */
209static inline bool ipc_valid_object(struct kern_ipc_perm *perm)
210{
Rafael Aquini72a8ff22014-01-27 17:07:02 -0800211 return !perm->deleted;
Rafael Aquini0f3d2b02014-01-27 17:07:01 -0800212}
213
Davidlohr Bueso4d2bff52013-04-30 19:15:19 -0700214struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
Pavel Emelyanovb2d75cd2008-02-08 04:18:54 -0800215int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700216 const struct ipc_ops *ops, struct ipc_params *params);
Alexey Dobriyan665c7742009-06-17 16:27:57 -0700217void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
218 void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
Al Viro553f7702017-07-08 22:52:47 -0400219
220#ifdef CONFIG_COMPAT
221#include <linux/compat.h>
222struct compat_ipc_perm {
223 key_t key;
224 __compat_uid_t uid;
225 __compat_gid_t gid;
226 __compat_uid_t cuid;
227 __compat_gid_t cgid;
228 compat_mode_t mode;
229 unsigned short seq;
230};
231
Al Viroc0ebccb2017-07-09 10:03:23 -0400232void to_compat_ipc_perm(struct compat_ipc_perm *, struct ipc64_perm *);
233void to_compat_ipc64_perm(struct compat_ipc64_perm *, struct ipc64_perm *);
234int get_compat_ipc_perm(struct ipc64_perm *, struct compat_ipc_perm __user *);
235int get_compat_ipc64_perm(struct ipc64_perm *,
236 struct compat_ipc64_perm __user *);
237
Al Viro553f7702017-07-08 22:52:47 -0400238static inline int compat_ipc_parse_version(int *cmd)
239{
240#ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
241 int version = *cmd & IPC_64;
242 *cmd &= ~IPC_64;
243 return version;
244#else
245 return IPC_64;
246#endif
247}
248#endif
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100249
250/* for __ARCH_WANT_SYS_IPC */
251long ksys_semtimedop(int semid, struct sembuf __user *tsops,
252 unsigned int nsops,
Arnd Bergmann21fc5382018-04-13 13:58:00 +0200253 const struct __kernel_timespec __user *timeout);
Dominik Brodowski69894712018-03-20 19:53:58 +0100254long ksys_semget(key_t key, int nsems, int semflg);
Dominik Brodowskid969c6f2018-03-20 20:00:39 +0100255long ksys_semctl(int semid, int semnum, int cmd, unsigned long arg);
Dominik Brodowski3d656612018-03-20 20:06:04 +0100256long ksys_msgget(key_t key, int msgflg);
Dominik Brodowskie340db52018-03-20 20:15:28 +0100257long ksys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf);
Dominik Brodowski078faac2018-03-20 21:25:57 +0100258long ksys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
259 long msgtyp, int msgflg);
Dominik Brodowski31c213f2018-03-20 21:29:00 +0100260long ksys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz,
261 int msgflg);
Dominik Brodowski65749e02018-03-20 20:07:53 +0100262long ksys_shmget(key_t key, size_t size, int shmflg);
Dominik Brodowskida1e27442018-03-20 20:09:48 +0100263long ksys_shmdt(char __user *shmaddr);
Dominik Brodowskic84d0792018-03-20 20:12:33 +0100264long ksys_shmctl(int shmid, int cmd, struct shmid_ds __user *buf);
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100265
266/* for CONFIG_ARCH_WANT_OLD_COMPAT_IPC */
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100267long compat_ksys_semtimedop(int semid, struct sembuf __user *tsems,
268 unsigned int nsops,
269 const struct compat_timespec __user *timeout);
Arnd Bergmannb0d17572018-04-13 13:58:23 +0200270#ifdef CONFIG_COMPAT
Dominik Brodowskid969c6f2018-03-20 20:00:39 +0100271long compat_ksys_semctl(int semid, int semnum, int cmd, int arg);
Dominik Brodowskie340db52018-03-20 20:15:28 +0100272long compat_ksys_msgctl(int msqid, int cmd, void __user *uptr);
Dominik Brodowski078faac2018-03-20 21:25:57 +0100273long compat_ksys_msgrcv(int msqid, compat_uptr_t msgp, compat_ssize_t msgsz,
274 compat_long_t msgtyp, int msgflg);
Dominik Brodowski31c213f2018-03-20 21:29:00 +0100275long compat_ksys_msgsnd(int msqid, compat_uptr_t msgp,
276 compat_ssize_t msgsz, int msgflg);
Dominik Brodowskic84d0792018-03-20 20:12:33 +0100277long compat_ksys_shmctl(int shmid, int cmd, void __user *uptr);
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100278#endif /* CONFIG_COMPAT */
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#endif