1 /*
2  * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3  * Copyright 2001-2006 Ian Kent <raven@themaw.net>
4  *
5  * This file is part of the Linux kernel and is made available under
6  * the terms of the GNU General Public License, version 2, or at your
7  * option, any later version, incorporated herein by reference.
8  */
9 
10 #include <linux/sched/signal.h>
11 #include "autofs_i.h"
12 
13 /* We make this a static variable rather than a part of the superblock; it
14  * is better if we don't reassign numbers easily even across filesystems
15  */
16 static autofs_wqt_t autofs_next_wait_queue = 1;
17 
autofs_catatonic_mode(struct autofs_sb_info * sbi)18 void autofs_catatonic_mode(struct autofs_sb_info *sbi)
19 {
20 	struct autofs_wait_queue *wq, *nwq;
21 
22 	mutex_lock(&sbi->wq_mutex);
23 	if (sbi->catatonic) {
24 		mutex_unlock(&sbi->wq_mutex);
25 		return;
26 	}
27 
28 	pr_debug("entering catatonic mode\n");
29 
30 	sbi->catatonic = 1;
31 	wq = sbi->queues;
32 	sbi->queues = NULL;	/* Erase all wait queues */
33 	while (wq) {
34 		nwq = wq->next;
35 		wq->status = -ENOENT; /* Magic is gone - report failure */
36 		kfree(wq->name.name);
37 		wq->name.name = NULL;
38 		wake_up_interruptible(&wq->queue);
39 		if (!--wq->wait_ctr)
40 			kfree(wq);
41 		wq = nwq;
42 	}
43 	fput(sbi->pipe);	/* Close the pipe */
44 	sbi->pipe = NULL;
45 	sbi->pipefd = -1;
46 	mutex_unlock(&sbi->wq_mutex);
47 }
48 
autofs_write(struct autofs_sb_info * sbi,struct file * file,const void * addr,int bytes)49 static int autofs_write(struct autofs_sb_info *sbi,
50 			struct file *file, const void *addr, int bytes)
51 {
52 	unsigned long sigpipe, flags;
53 	const char *data = (const char *)addr;
54 	ssize_t wr = 0;
55 
56 	sigpipe = sigismember(&current->pending.signal, SIGPIPE);
57 
58 	mutex_lock(&sbi->pipe_mutex);
59 	while (bytes) {
60 		wr = __kernel_write(file, data, bytes, &file->f_pos);
61 		if (wr <= 0)
62 			break;
63 		data += wr;
64 		bytes -= wr;
65 	}
66 	mutex_unlock(&sbi->pipe_mutex);
67 
68 	/* Keep the currently executing process from receiving a
69 	 * SIGPIPE unless it was already supposed to get one
70 	 */
71 	if (wr == -EPIPE && !sigpipe) {
72 		spin_lock_irqsave(&current->sighand->siglock, flags);
73 		sigdelset(&current->pending.signal, SIGPIPE);
74 		recalc_sigpending();
75 		spin_unlock_irqrestore(&current->sighand->siglock, flags);
76 	}
77 
78 	/* if 'wr' returned 0 (impossible) we assume -EIO (safe) */
79 	return bytes == 0 ? 0 : wr < 0 ? wr : -EIO;
80 }
81 
autofs_notify_daemon(struct autofs_sb_info * sbi,struct autofs_wait_queue * wq,int type)82 static void autofs_notify_daemon(struct autofs_sb_info *sbi,
83 				 struct autofs_wait_queue *wq,
84 				 int type)
85 {
86 	union {
87 		struct autofs_packet_hdr hdr;
88 		union autofs_packet_union v4_pkt;
89 		union autofs_v5_packet_union v5_pkt;
90 	} pkt;
91 	struct file *pipe = NULL;
92 	size_t pktsz;
93 	int ret;
94 
95 	pr_debug("wait id = 0x%08lx, name = %.*s, type=%d\n",
96 		 (unsigned long) wq->wait_queue_token,
97 		 wq->name.len, wq->name.name, type);
98 
99 	memset(&pkt, 0, sizeof(pkt)); /* For security reasons */
100 
101 	pkt.hdr.proto_version = sbi->version;
102 	pkt.hdr.type = type;
103 
104 	switch (type) {
105 	/* Kernel protocol v4 missing and expire packets */
106 	case autofs_ptype_missing:
107 	{
108 		struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
109 
110 		pktsz = sizeof(*mp);
111 
112 		mp->wait_queue_token = wq->wait_queue_token;
113 		mp->len = wq->name.len;
114 		memcpy(mp->name, wq->name.name, wq->name.len);
115 		mp->name[wq->name.len] = '\0';
116 		break;
117 	}
118 	case autofs_ptype_expire_multi:
119 	{
120 		struct autofs_packet_expire_multi *ep =
121 					&pkt.v4_pkt.expire_multi;
122 
123 		pktsz = sizeof(*ep);
124 
125 		ep->wait_queue_token = wq->wait_queue_token;
126 		ep->len = wq->name.len;
127 		memcpy(ep->name, wq->name.name, wq->name.len);
128 		ep->name[wq->name.len] = '\0';
129 		break;
130 	}
131 	/*
132 	 * Kernel protocol v5 packet for handling indirect and direct
133 	 * mount missing and expire requests
134 	 */
135 	case autofs_ptype_missing_indirect:
136 	case autofs_ptype_expire_indirect:
137 	case autofs_ptype_missing_direct:
138 	case autofs_ptype_expire_direct:
139 	{
140 		struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
141 		struct user_namespace *user_ns = sbi->pipe->f_cred->user_ns;
142 
143 		pktsz = sizeof(*packet);
144 
145 		packet->wait_queue_token = wq->wait_queue_token;
146 		packet->len = wq->name.len;
147 		memcpy(packet->name, wq->name.name, wq->name.len);
148 		packet->name[wq->name.len] = '\0';
149 		packet->dev = wq->dev;
150 		packet->ino = wq->ino;
151 		packet->uid = from_kuid_munged(user_ns, wq->uid);
152 		packet->gid = from_kgid_munged(user_ns, wq->gid);
153 		packet->pid = wq->pid;
154 		packet->tgid = wq->tgid;
155 		break;
156 	}
157 	default:
158 		pr_warn("bad type %d!\n", type);
159 		mutex_unlock(&sbi->wq_mutex);
160 		return;
161 	}
162 
163 	pipe = get_file(sbi->pipe);
164 
165 	mutex_unlock(&sbi->wq_mutex);
166 
167 	switch (ret = autofs_write(sbi, pipe, &pkt, pktsz)) {
168 	case 0:
169 		break;
170 	case -ENOMEM:
171 	case -ERESTARTSYS:
172 		/* Just fail this one */
173 		autofs_wait_release(sbi, wq->wait_queue_token, ret);
174 		break;
175 	default:
176 		autofs_catatonic_mode(sbi);
177 		break;
178 	}
179 	fput(pipe);
180 }
181 
autofs_getpath(struct autofs_sb_info * sbi,struct dentry * dentry,char * name)182 static int autofs_getpath(struct autofs_sb_info *sbi,
183 			  struct dentry *dentry, char *name)
184 {
185 	struct dentry *root = sbi->sb->s_root;
186 	struct dentry *tmp;
187 	char *buf;
188 	char *p;
189 	int len;
190 	unsigned seq;
191 
192 rename_retry:
193 	buf = name;
194 	len = 0;
195 
196 	seq = read_seqbegin(&rename_lock);
197 	rcu_read_lock();
198 	spin_lock(&sbi->fs_lock);
199 	for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
200 		len += tmp->d_name.len + 1;
201 
202 	if (!len || --len > NAME_MAX) {
203 		spin_unlock(&sbi->fs_lock);
204 		rcu_read_unlock();
205 		if (read_seqretry(&rename_lock, seq))
206 			goto rename_retry;
207 		return 0;
208 	}
209 
210 	*(buf + len) = '\0';
211 	p = buf + len - dentry->d_name.len;
212 	strncpy(p, dentry->d_name.name, dentry->d_name.len);
213 
214 	for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
215 		*(--p) = '/';
216 		p -= tmp->d_name.len;
217 		strncpy(p, tmp->d_name.name, tmp->d_name.len);
218 	}
219 	spin_unlock(&sbi->fs_lock);
220 	rcu_read_unlock();
221 	if (read_seqretry(&rename_lock, seq))
222 		goto rename_retry;
223 
224 	return len;
225 }
226 
227 static struct autofs_wait_queue *
autofs_find_wait(struct autofs_sb_info * sbi,const struct qstr * qstr)228 autofs_find_wait(struct autofs_sb_info *sbi, const struct qstr *qstr)
229 {
230 	struct autofs_wait_queue *wq;
231 
232 	for (wq = sbi->queues; wq; wq = wq->next) {
233 		if (wq->name.hash == qstr->hash &&
234 		    wq->name.len == qstr->len &&
235 		    wq->name.name &&
236 		    !memcmp(wq->name.name, qstr->name, qstr->len))
237 			break;
238 	}
239 	return wq;
240 }
241 
242 /*
243  * Check if we have a valid request.
244  * Returns
245  * 1 if the request should continue.
246  *   In this case we can return an autofs_wait_queue entry if one is
247  *   found or NULL to idicate a new wait needs to be created.
248  * 0 or a negative errno if the request shouldn't continue.
249  */
validate_request(struct autofs_wait_queue ** wait,struct autofs_sb_info * sbi,const struct qstr * qstr,const struct path * path,enum autofs_notify notify)250 static int validate_request(struct autofs_wait_queue **wait,
251 			    struct autofs_sb_info *sbi,
252 			    const struct qstr *qstr,
253 			    const struct path *path, enum autofs_notify notify)
254 {
255 	struct dentry *dentry = path->dentry;
256 	struct autofs_wait_queue *wq;
257 	struct autofs_info *ino;
258 
259 	if (sbi->catatonic)
260 		return -ENOENT;
261 
262 	/* Wait in progress, continue; */
263 	wq = autofs_find_wait(sbi, qstr);
264 	if (wq) {
265 		*wait = wq;
266 		return 1;
267 	}
268 
269 	*wait = NULL;
270 
271 	/* If we don't yet have any info this is a new request */
272 	ino = autofs_dentry_ino(dentry);
273 	if (!ino)
274 		return 1;
275 
276 	/*
277 	 * If we've been asked to wait on an existing expire (NFY_NONE)
278 	 * but there is no wait in the queue ...
279 	 */
280 	if (notify == NFY_NONE) {
281 		/*
282 		 * Either we've betean the pending expire to post it's
283 		 * wait or it finished while we waited on the mutex.
284 		 * So we need to wait till either, the wait appears
285 		 * or the expire finishes.
286 		 */
287 
288 		while (ino->flags & AUTOFS_INF_EXPIRING) {
289 			mutex_unlock(&sbi->wq_mutex);
290 			schedule_timeout_interruptible(HZ/10);
291 			if (mutex_lock_interruptible(&sbi->wq_mutex))
292 				return -EINTR;
293 
294 			if (sbi->catatonic)
295 				return -ENOENT;
296 
297 			wq = autofs_find_wait(sbi, qstr);
298 			if (wq) {
299 				*wait = wq;
300 				return 1;
301 			}
302 		}
303 
304 		/*
305 		 * Not ideal but the status has already gone. Of the two
306 		 * cases where we wait on NFY_NONE neither depend on the
307 		 * return status of the wait.
308 		 */
309 		return 0;
310 	}
311 
312 	/*
313 	 * If we've been asked to trigger a mount and the request
314 	 * completed while we waited on the mutex ...
315 	 */
316 	if (notify == NFY_MOUNT) {
317 		struct dentry *new = NULL;
318 		struct path this;
319 		int valid = 1;
320 
321 		/*
322 		 * If the dentry was successfully mounted while we slept
323 		 * on the wait queue mutex we can return success. If it
324 		 * isn't mounted (doesn't have submounts for the case of
325 		 * a multi-mount with no mount at it's base) we can
326 		 * continue on and create a new request.
327 		 */
328 		if (!IS_ROOT(dentry)) {
329 			if (d_unhashed(dentry) &&
330 			    d_really_is_positive(dentry)) {
331 				struct dentry *parent = dentry->d_parent;
332 
333 				new = d_lookup(parent, &dentry->d_name);
334 				if (new)
335 					dentry = new;
336 			}
337 		}
338 		this.mnt = path->mnt;
339 		this.dentry = dentry;
340 		if (path_has_submounts(&this))
341 			valid = 0;
342 
343 		if (new)
344 			dput(new);
345 		return valid;
346 	}
347 
348 	return 1;
349 }
350 
autofs_wait(struct autofs_sb_info * sbi,const struct path * path,enum autofs_notify notify)351 int autofs_wait(struct autofs_sb_info *sbi,
352 		 const struct path *path, enum autofs_notify notify)
353 {
354 	struct dentry *dentry = path->dentry;
355 	struct autofs_wait_queue *wq;
356 	struct qstr qstr;
357 	char *name;
358 	int status, ret, type;
359 	pid_t pid;
360 	pid_t tgid;
361 
362 	/* In catatonic mode, we don't wait for nobody */
363 	if (sbi->catatonic)
364 		return -ENOENT;
365 
366 	/*
367 	 * Try translating pids to the namespace of the daemon.
368 	 *
369 	 * Zero means failure: we are in an unrelated pid namespace.
370 	 */
371 	pid = task_pid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
372 	tgid = task_tgid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
373 	if (pid == 0 || tgid == 0)
374 		return -ENOENT;
375 
376 	if (d_really_is_negative(dentry)) {
377 		/*
378 		 * A wait for a negative dentry is invalid for certain
379 		 * cases. A direct or offset mount "always" has its mount
380 		 * point directory created and so the request dentry must
381 		 * be positive or the map key doesn't exist. The situation
382 		 * is very similar for indirect mounts except only dentrys
383 		 * in the root of the autofs file system may be negative.
384 		 */
385 		if (autofs_type_trigger(sbi->type))
386 			return -ENOENT;
387 		else if (!IS_ROOT(dentry->d_parent))
388 			return -ENOENT;
389 	}
390 
391 	name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
392 	if (!name)
393 		return -ENOMEM;
394 
395 	/* If this is a direct mount request create a dummy name */
396 	if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type))
397 		qstr.len = sprintf(name, "%p", dentry);
398 	else {
399 		qstr.len = autofs_getpath(sbi, dentry, name);
400 		if (!qstr.len) {
401 			kfree(name);
402 			return -ENOENT;
403 		}
404 	}
405 	qstr.name = name;
406 	qstr.hash = full_name_hash(dentry, name, qstr.len);
407 
408 	if (mutex_lock_interruptible(&sbi->wq_mutex)) {
409 		kfree(qstr.name);
410 		return -EINTR;
411 	}
412 
413 	ret = validate_request(&wq, sbi, &qstr, path, notify);
414 	if (ret <= 0) {
415 		if (ret != -EINTR)
416 			mutex_unlock(&sbi->wq_mutex);
417 		kfree(qstr.name);
418 		return ret;
419 	}
420 
421 	if (!wq) {
422 		/* Create a new wait queue */
423 		wq = kmalloc(sizeof(struct autofs_wait_queue), GFP_KERNEL);
424 		if (!wq) {
425 			kfree(qstr.name);
426 			mutex_unlock(&sbi->wq_mutex);
427 			return -ENOMEM;
428 		}
429 
430 		wq->wait_queue_token = autofs_next_wait_queue;
431 		if (++autofs_next_wait_queue == 0)
432 			autofs_next_wait_queue = 1;
433 		wq->next = sbi->queues;
434 		sbi->queues = wq;
435 		init_waitqueue_head(&wq->queue);
436 		memcpy(&wq->name, &qstr, sizeof(struct qstr));
437 		wq->dev = autofs_get_dev(sbi);
438 		wq->ino = autofs_get_ino(sbi);
439 		wq->uid = current_uid();
440 		wq->gid = current_gid();
441 		wq->pid = pid;
442 		wq->tgid = tgid;
443 		wq->status = -EINTR; /* Status return if interrupted */
444 		wq->wait_ctr = 2;
445 
446 		if (sbi->version < 5) {
447 			if (notify == NFY_MOUNT)
448 				type = autofs_ptype_missing;
449 			else
450 				type = autofs_ptype_expire_multi;
451 		} else {
452 			if (notify == NFY_MOUNT)
453 				type = autofs_type_trigger(sbi->type) ?
454 					autofs_ptype_missing_direct :
455 					 autofs_ptype_missing_indirect;
456 			else
457 				type = autofs_type_trigger(sbi->type) ?
458 					autofs_ptype_expire_direct :
459 					autofs_ptype_expire_indirect;
460 		}
461 
462 		pr_debug("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
463 			 (unsigned long) wq->wait_queue_token, wq->name.len,
464 			 wq->name.name, notify);
465 
466 		/*
467 		 * autofs_notify_daemon() may block; it will unlock ->wq_mutex
468 		 */
469 		autofs_notify_daemon(sbi, wq, type);
470 	} else {
471 		wq->wait_ctr++;
472 		pr_debug("existing wait id = 0x%08lx, name = %.*s, nfy=%d\n",
473 			 (unsigned long) wq->wait_queue_token, wq->name.len,
474 			 wq->name.name, notify);
475 		mutex_unlock(&sbi->wq_mutex);
476 		kfree(qstr.name);
477 	}
478 
479 	/*
480 	 * wq->name.name is NULL iff the lock is already released
481 	 * or the mount has been made catatonic.
482 	 */
483 	wait_event_killable(wq->queue, wq->name.name == NULL);
484 	status = wq->status;
485 
486 	/*
487 	 * For direct and offset mounts we need to track the requester's
488 	 * uid and gid in the dentry info struct. This is so it can be
489 	 * supplied, on request, by the misc device ioctl interface.
490 	 * This is needed during daemon resatart when reconnecting
491 	 * to existing, active, autofs mounts. The uid and gid (and
492 	 * related string values) may be used for macro substitution
493 	 * in autofs mount maps.
494 	 */
495 	if (!status) {
496 		struct autofs_info *ino;
497 		struct dentry *de = NULL;
498 
499 		/* direct mount or browsable map */
500 		ino = autofs_dentry_ino(dentry);
501 		if (!ino) {
502 			/* If not lookup actual dentry used */
503 			de = d_lookup(dentry->d_parent, &dentry->d_name);
504 			if (de)
505 				ino = autofs_dentry_ino(de);
506 		}
507 
508 		/* Set mount requester */
509 		if (ino) {
510 			spin_lock(&sbi->fs_lock);
511 			ino->uid = wq->uid;
512 			ino->gid = wq->gid;
513 			spin_unlock(&sbi->fs_lock);
514 		}
515 
516 		if (de)
517 			dput(de);
518 	}
519 
520 	/* Are we the last process to need status? */
521 	mutex_lock(&sbi->wq_mutex);
522 	if (!--wq->wait_ctr)
523 		kfree(wq);
524 	mutex_unlock(&sbi->wq_mutex);
525 
526 	return status;
527 }
528 
529 
autofs_wait_release(struct autofs_sb_info * sbi,autofs_wqt_t wait_queue_token,int status)530 int autofs_wait_release(struct autofs_sb_info *sbi,
531 			autofs_wqt_t wait_queue_token, int status)
532 {
533 	struct autofs_wait_queue *wq, **wql;
534 
535 	mutex_lock(&sbi->wq_mutex);
536 	for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
537 		if (wq->wait_queue_token == wait_queue_token)
538 			break;
539 	}
540 
541 	if (!wq) {
542 		mutex_unlock(&sbi->wq_mutex);
543 		return -EINVAL;
544 	}
545 
546 	*wql = wq->next;	/* Unlink from chain */
547 	kfree(wq->name.name);
548 	wq->name.name = NULL;	/* Do not wait on this queue */
549 	wq->status = status;
550 	wake_up(&wq->queue);
551 	if (!--wq->wait_ctr)
552 		kfree(wq);
553 	mutex_unlock(&sbi->wq_mutex);
554 
555 	return 0;
556 }
557