aboutsummaryrefslogtreecommitdiffstats
path: root/ipc/util.h
diff options
context:
space:
mode:
Diffstat (limited to 'ipc/util.h')
-rw-r--r--ipc/util.h168
1 files changed, 135 insertions, 33 deletions
diff --git a/ipc/util.h b/ipc/util.h
index 333e891bcaca..9ffea40457ce 100644
--- a/ipc/util.h
+++ b/ipc/util.h
@@ -10,6 +10,9 @@
10#ifndef _IPC_UTIL_H 10#ifndef _IPC_UTIL_H
11#define _IPC_UTIL_H 11#define _IPC_UTIL_H
12 12
13#include <linux/idr.h>
14#include <linux/err.h>
15
13#define USHRT_MAX 0xffff 16#define USHRT_MAX 0xffff
14#define SEQ_MULTIPLIER (IPCMNI) 17#define SEQ_MULTIPLIER (IPCMNI)
15 18
@@ -25,24 +28,46 @@ void sem_exit_ns(struct ipc_namespace *ns);
25void msg_exit_ns(struct ipc_namespace *ns); 28void msg_exit_ns(struct ipc_namespace *ns);
26void shm_exit_ns(struct ipc_namespace *ns); 29void shm_exit_ns(struct ipc_namespace *ns);
27 30
28struct ipc_id_ary {
29 int size;
30 struct kern_ipc_perm *p[0];
31};
32
33struct ipc_ids { 31struct ipc_ids {
34 int in_use; 32 int in_use;
35 int max_id;
36 unsigned short seq; 33 unsigned short seq;
37 unsigned short seq_max; 34 unsigned short seq_max;
38 struct mutex mutex; 35 struct rw_semaphore rw_mutex;
39 struct ipc_id_ary nullentry; 36 struct idr ipcs_idr;
40 struct ipc_id_ary* entries; 37};
38
39/*
40 * Structure that holds the parameters needed by the ipc operations
41 * (see after)
42 */
43struct ipc_params {
44 key_t key;
45 int flg;
46 union {
47 size_t size; /* for shared memories */
48 int nsems; /* for semaphores */
49 } u; /* holds the getnew() specific param */
50};
51
52/*
53 * Structure that holds some ipc operations. This structure is used to unify
54 * the calls to sys_msgget(), sys_semget(), sys_shmget()
55 * . routine to call to create a new ipc object. Can be one of newque,
56 * newary, newseg
57 * . routine to call to check permissions for a new ipc object.
58 * Can be one of security_msg_associate, security_sem_associate,
59 * security_shm_associate
60 * . routine to call for an extra check if needed
61 */
62struct ipc_ops {
63 int (*getnew) (struct ipc_namespace *, struct ipc_params *);
64 int (*associate) (struct kern_ipc_perm *, int);
65 int (*more_checks) (struct kern_ipc_perm *, struct ipc_params *);
41}; 66};
42 67
43struct seq_file; 68struct seq_file;
44 69
45void ipc_init_ids(struct ipc_ids *ids, int size); 70void ipc_init_ids(struct ipc_ids *);
46#ifdef CONFIG_PROC_FS 71#ifdef CONFIG_PROC_FS
47void __init ipc_init_proc_interface(const char *path, const char *header, 72void __init ipc_init_proc_interface(const char *path, const char *header,
48 int ids, int (*show)(struct seq_file *, void *)); 73 int ids, int (*show)(struct seq_file *, void *));
@@ -54,14 +79,19 @@ void __init ipc_init_proc_interface(const char *path, const char *header,
54#define IPC_MSG_IDS 1 79#define IPC_MSG_IDS 1
55#define IPC_SHM_IDS 2 80#define IPC_SHM_IDS 2
56 81
57/* must be called with ids->mutex acquired.*/ 82#define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
58int ipc_findkey(struct ipc_ids* ids, key_t key); 83
59int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size); 84/* must be called with ids->rw_mutex acquired for writing */
85int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
86
87/* must be called with ids->rw_mutex acquired for reading */
88int ipc_get_maxid(struct ipc_ids *);
60 89
61/* must be called with both locks acquired. */ 90/* must be called with both locks acquired. */
62struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id); 91void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
63 92
64int ipcperms (struct kern_ipc_perm *ipcp, short flg); 93/* must be called with ipcp locked */
94int ipcperms(struct kern_ipc_perm *ipcp, short flg);
65 95
66/* for rare, potentially huge allocations. 96/* for rare, potentially huge allocations.
67 * both function can sleep 97 * both function can sleep
@@ -79,24 +109,12 @@ void* ipc_rcu_alloc(int size);
79void ipc_rcu_getref(void *ptr); 109void ipc_rcu_getref(void *ptr);
80void ipc_rcu_putref(void *ptr); 110void ipc_rcu_putref(void *ptr);
81 111
82static inline void __ipc_fini_ids(struct ipc_ids *ids, 112/*
83 struct ipc_id_ary *entries) 113 * ipc_lock_down: called with rw_mutex held
84{ 114 * ipc_lock: called without that lock held
85 if (entries != &ids->nullentry) 115 */
86 ipc_rcu_putref(entries); 116struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *, int);
87} 117struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
88
89static inline void ipc_fini_ids(struct ipc_ids *ids)
90{
91 __ipc_fini_ids(ids, ids->entries);
92}
93
94struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id);
95struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id);
96void ipc_lock_by_ptr(struct kern_ipc_perm *ipcp);
97void ipc_unlock(struct kern_ipc_perm* perm);
98int ipc_buildid(struct ipc_ids* ids, int id, int seq);
99int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid);
100 118
101void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out); 119void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
102void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out); 120void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
@@ -111,5 +129,89 @@ int ipc_parse_version (int *cmd);
111extern void free_msg(struct msg_msg *msg); 129extern void free_msg(struct msg_msg *msg);
112extern struct msg_msg *load_msg(const void __user *src, int len); 130extern struct msg_msg *load_msg(const void __user *src, int len);
113extern int store_msg(void __user *dest, struct msg_msg *msg, int len); 131extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
132extern int ipcget_new(struct ipc_namespace *, struct ipc_ids *,
133 struct ipc_ops *, struct ipc_params *);
134extern int ipcget_public(struct ipc_namespace *, struct ipc_ids *,
135 struct ipc_ops *, struct ipc_params *);
136
137static inline int ipc_buildid(int id, int seq)
138{
139 return SEQ_MULTIPLIER * seq + id;
140}
141
142/*
143 * Must be called with ipcp locked
144 */
145static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid)
146{
147 if (uid / SEQ_MULTIPLIER != ipcp->seq)
148 return 1;
149 return 0;
150}
151
152static inline void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
153{
154 rcu_read_lock();
155 spin_lock(&perm->lock);
156}
157
158static inline void ipc_unlock(struct kern_ipc_perm *perm)
159{
160 spin_unlock(&perm->lock);
161 rcu_read_unlock();
162}
163
164static inline struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids,
165 int id)
166{
167 struct kern_ipc_perm *out;
168
169 out = ipc_lock_down(ids, id);
170 if (IS_ERR(out))
171 return out;
172
173 if (ipc_checkid(out, id)) {
174 ipc_unlock(out);
175 return ERR_PTR(-EIDRM);
176 }
177
178 return out;
179}
180
181static inline struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids,
182 int id)
183{
184 struct kern_ipc_perm *out;
185
186 out = ipc_lock(ids, id);
187 if (IS_ERR(out))
188 return out;
189
190 if (ipc_checkid(out, id)) {
191 ipc_unlock(out);
192 return ERR_PTR(-EIDRM);
193 }
194
195 return out;
196}
197
198/**
199 * ipcget - Common sys_*get() code
200 * @ns : namsepace
201 * @ids : IPC identifier set
202 * @ops : operations to be called on ipc object creation, permission checks
203 * and further checks
204 * @params : the parameters needed by the previous operations.
205 *
206 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
207 */
208static inline int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
209 struct ipc_ops *ops, struct ipc_params *params)
210{
211 if (params->key == IPC_PRIVATE)
212 return ipcget_new(ns, ids, ops, params);
213 else
214 return ipcget_public(ns, ids, ops, params);
215}
114 216
115#endif 217#endif