33
33
#include <asm/siginfo.h>
34
34
#include <linux/uaccess.h>
35
35
36
+ #include "internal.h"
37
+
36
38
#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
37
39
38
40
static int setfl (int fd , struct file * filp , unsigned int arg )
@@ -87,22 +89,64 @@ static int setfl(int fd, struct file * filp, unsigned int arg)
87
89
return error ;
88
90
}
89
91
92
+ /*
93
+ * Allocate an file->f_owner struct if it doesn't exist, handling racing
94
+ * allocations correctly.
95
+ */
96
+ int file_f_owner_allocate (struct file * file )
97
+ {
98
+ struct fown_struct * f_owner ;
99
+
100
+ f_owner = file_f_owner (file );
101
+ if (f_owner )
102
+ return 0 ;
103
+
104
+ f_owner = kzalloc (sizeof (struct fown_struct ), GFP_KERNEL );
105
+ if (!f_owner )
106
+ return - ENOMEM ;
107
+
108
+ rwlock_init (& f_owner -> lock );
109
+ f_owner -> file = file ;
110
+ /* If someone else raced us, drop our allocation. */
111
+ if (unlikely (cmpxchg (& file -> f_owner , NULL , f_owner )))
112
+ kfree (f_owner );
113
+ return 0 ;
114
+ }
115
+ EXPORT_SYMBOL (file_f_owner_allocate );
116
+
117
+ void file_f_owner_release (struct file * file )
118
+ {
119
+ struct fown_struct * f_owner ;
120
+
121
+ f_owner = file_f_owner (file );
122
+ if (f_owner ) {
123
+ put_pid (f_owner -> pid );
124
+ kfree (f_owner );
125
+ }
126
+ }
127
+
90
128
static void f_modown (struct file * filp , struct pid * pid , enum pid_type type ,
91
129
int force )
92
130
{
93
- write_lock_irq (& filp -> f_owner .lock );
94
- if (force || !filp -> f_owner .pid ) {
95
- put_pid (filp -> f_owner .pid );
96
- filp -> f_owner .pid = get_pid (pid );
97
- filp -> f_owner .pid_type = type ;
131
+ struct fown_struct * f_owner ;
132
+
133
+ f_owner = file_f_owner (filp );
134
+ if (WARN_ON_ONCE (!f_owner ))
135
+ return ;
136
+
137
+ write_lock_irq (& f_owner -> lock );
138
+ if (force || !f_owner -> pid ) {
139
+ put_pid (f_owner -> pid );
140
+ f_owner -> pid = get_pid (pid );
141
+ f_owner -> pid_type = type ;
98
142
99
143
if (pid ) {
100
144
const struct cred * cred = current_cred ();
101
- filp -> f_owner . uid = cred -> uid ;
102
- filp -> f_owner . euid = cred -> euid ;
145
+ f_owner -> uid = cred -> uid ;
146
+ f_owner -> euid = cred -> euid ;
103
147
}
104
148
}
105
- write_unlock_irq (& filp -> f_owner . lock );
149
+ write_unlock_irq (& f_owner -> lock );
106
150
}
107
151
108
152
void __f_setown (struct file * filp , struct pid * pid , enum pid_type type ,
@@ -119,6 +163,8 @@ int f_setown(struct file *filp, int who, int force)
119
163
struct pid * pid = NULL ;
120
164
int ret = 0 ;
121
165
166
+ might_sleep ();
167
+
122
168
type = PIDTYPE_TGID ;
123
169
if (who < 0 ) {
124
170
/* avoid overflow below */
@@ -129,6 +175,10 @@ int f_setown(struct file *filp, int who, int force)
129
175
who = - who ;
130
176
}
131
177
178
+ ret = file_f_owner_allocate (filp );
179
+ if (ret )
180
+ return ret ;
181
+
132
182
rcu_read_lock ();
133
183
if (who ) {
134
184
pid = find_vpid (who );
@@ -152,16 +202,21 @@ void f_delown(struct file *filp)
152
202
pid_t f_getown (struct file * filp )
153
203
{
154
204
pid_t pid = 0 ;
205
+ struct fown_struct * f_owner ;
206
+
207
+ f_owner = file_f_owner (filp );
208
+ if (!f_owner )
209
+ return pid ;
155
210
156
- read_lock_irq (& filp -> f_owner . lock );
211
+ read_lock_irq (& f_owner -> lock );
157
212
rcu_read_lock ();
158
- if (pid_task (filp -> f_owner . pid , filp -> f_owner . pid_type )) {
159
- pid = pid_vnr (filp -> f_owner . pid );
160
- if (filp -> f_owner . pid_type == PIDTYPE_PGID )
213
+ if (pid_task (f_owner -> pid , f_owner -> pid_type )) {
214
+ pid = pid_vnr (f_owner -> pid );
215
+ if (f_owner -> pid_type == PIDTYPE_PGID )
161
216
pid = - pid ;
162
217
}
163
218
rcu_read_unlock ();
164
- read_unlock_irq (& filp -> f_owner . lock );
219
+ read_unlock_irq (& f_owner -> lock );
165
220
return pid ;
166
221
}
167
222
@@ -194,6 +249,10 @@ static int f_setown_ex(struct file *filp, unsigned long arg)
194
249
return - EINVAL ;
195
250
}
196
251
252
+ ret = file_f_owner_allocate (filp );
253
+ if (ret )
254
+ return ret ;
255
+
197
256
rcu_read_lock ();
198
257
pid = find_vpid (owner .pid );
199
258
if (owner .pid && !pid )
@@ -210,13 +269,20 @@ static int f_getown_ex(struct file *filp, unsigned long arg)
210
269
struct f_owner_ex __user * owner_p = (void __user * )arg ;
211
270
struct f_owner_ex owner = {};
212
271
int ret = 0 ;
272
+ struct fown_struct * f_owner ;
273
+ enum pid_type pid_type = PIDTYPE_PID ;
213
274
214
- read_lock_irq (& filp -> f_owner .lock );
215
- rcu_read_lock ();
216
- if (pid_task (filp -> f_owner .pid , filp -> f_owner .pid_type ))
217
- owner .pid = pid_vnr (filp -> f_owner .pid );
218
- rcu_read_unlock ();
219
- switch (filp -> f_owner .pid_type ) {
275
+ f_owner = file_f_owner (filp );
276
+ if (f_owner ) {
277
+ read_lock_irq (& f_owner -> lock );
278
+ rcu_read_lock ();
279
+ if (pid_task (f_owner -> pid , f_owner -> pid_type ))
280
+ owner .pid = pid_vnr (f_owner -> pid );
281
+ rcu_read_unlock ();
282
+ pid_type = f_owner -> pid_type ;
283
+ }
284
+
285
+ switch (pid_type ) {
220
286
case PIDTYPE_PID :
221
287
owner .type = F_OWNER_TID ;
222
288
break ;
@@ -234,7 +300,8 @@ static int f_getown_ex(struct file *filp, unsigned long arg)
234
300
ret = - EINVAL ;
235
301
break ;
236
302
}
237
- read_unlock_irq (& filp -> f_owner .lock );
303
+ if (f_owner )
304
+ read_unlock_irq (& f_owner -> lock );
238
305
239
306
if (!ret ) {
240
307
ret = copy_to_user (owner_p , & owner , sizeof (owner ));
@@ -248,14 +315,18 @@ static int f_getown_ex(struct file *filp, unsigned long arg)
248
315
static int f_getowner_uids (struct file * filp , unsigned long arg )
249
316
{
250
317
struct user_namespace * user_ns = current_user_ns ();
318
+ struct fown_struct * f_owner ;
251
319
uid_t __user * dst = (void __user * )arg ;
252
- uid_t src [2 ];
320
+ uid_t src [2 ] = { 0 , 0 } ;
253
321
int err ;
254
322
255
- read_lock_irq (& filp -> f_owner .lock );
256
- src [0 ] = from_kuid (user_ns , filp -> f_owner .uid );
257
- src [1 ] = from_kuid (user_ns , filp -> f_owner .euid );
258
- read_unlock_irq (& filp -> f_owner .lock );
323
+ f_owner = file_f_owner (filp );
324
+ if (f_owner ) {
325
+ read_lock_irq (& f_owner -> lock );
326
+ src [0 ] = from_kuid (user_ns , f_owner -> uid );
327
+ src [1 ] = from_kuid (user_ns , f_owner -> euid );
328
+ read_unlock_irq (& f_owner -> lock );
329
+ }
259
330
260
331
err = put_user (src [0 ], & dst [0 ]);
261
332
err |= put_user (src [1 ], & dst [1 ]);
@@ -343,6 +414,30 @@ static long f_dupfd_query(int fd, struct file *filp)
343
414
return f .file == filp ;
344
415
}
345
416
417
+ static int f_owner_sig (struct file * filp , int signum , bool setsig )
418
+ {
419
+ int ret = 0 ;
420
+ struct fown_struct * f_owner ;
421
+
422
+ might_sleep ();
423
+
424
+ if (setsig ) {
425
+ if (!valid_signal (signum ))
426
+ return - EINVAL ;
427
+
428
+ ret = file_f_owner_allocate (filp );
429
+ if (ret )
430
+ return ret ;
431
+ }
432
+
433
+ f_owner = file_f_owner (filp );
434
+ if (setsig )
435
+ f_owner -> signum = signum ;
436
+ else if (f_owner )
437
+ ret = f_owner -> signum ;
438
+ return ret ;
439
+ }
440
+
346
441
static long do_fcntl (int fd , unsigned int cmd , unsigned long arg ,
347
442
struct file * filp )
348
443
{
@@ -421,15 +516,10 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
421
516
err = f_getowner_uids (filp , arg );
422
517
break ;
423
518
case F_GETSIG :
424
- err = filp -> f_owner . signum ;
519
+ err = f_owner_sig ( filp , 0 , false) ;
425
520
break ;
426
521
case F_SETSIG :
427
- /* arg == 0 restores default behaviour. */
428
- if (!valid_signal (argi )) {
429
- break ;
430
- }
431
- err = 0 ;
432
- filp -> f_owner .signum = argi ;
522
+ err = f_owner_sig (filp , argi , true);
433
523
break ;
434
524
case F_GETLEASE :
435
525
err = fcntl_getlease (filp );
@@ -844,14 +934,19 @@ static void send_sigurg_to_task(struct task_struct *p,
844
934
do_send_sig_info (SIGURG , SEND_SIG_PRIV , p , type );
845
935
}
846
936
847
- int send_sigurg (struct fown_struct * fown )
937
+ int send_sigurg (struct file * file )
848
938
{
939
+ struct fown_struct * fown ;
849
940
struct task_struct * p ;
850
941
enum pid_type type ;
851
942
struct pid * pid ;
852
943
unsigned long flags ;
853
944
int ret = 0 ;
854
945
946
+ fown = file_f_owner (file );
947
+ if (!fown )
948
+ return 0 ;
949
+
855
950
read_lock_irqsave (& fown -> lock , flags );
856
951
857
952
type = fown -> pid_type ;
@@ -1027,13 +1122,16 @@ static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
1027
1122
}
1028
1123
read_lock_irqsave (& fa -> fa_lock , flags );
1029
1124
if (fa -> fa_file ) {
1030
- fown = & fa -> fa_file -> f_owner ;
1125
+ fown = file_f_owner (fa -> fa_file );
1126
+ if (!fown )
1127
+ goto next ;
1031
1128
/* Don't send SIGURG to processes which have not set a
1032
1129
queued signum: SIGURG has its own default signalling
1033
1130
mechanism. */
1034
1131
if (!(sig == SIGURG && fown -> signum == 0 ))
1035
1132
send_sigio (fown , fa -> fa_fd , band );
1036
1133
}
1134
+ next :
1037
1135
read_unlock_irqrestore (& fa -> fa_lock , flags );
1038
1136
fa = rcu_dereference (fa -> fa_next );
1039
1137
}
0 commit comments