1435 lines · 43.2 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* *  file.c - part of debugfs, a tiny little debug file system * *  Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> *  Copyright (C) 2004 IBM Inc. * *  debugfs is for people to use instead of /proc or /sys. *  See Documentation/filesystems/ for more details. */ #include <linux/module.h>#include <linux/fs.h>#include <linux/seq_file.h>#include <linux/pagemap.h>#include <linux/debugfs.h>#include <linux/io.h>#include <linux/slab.h>#include <linux/atomic.h>#include <linux/device.h>#include <linux/pm_runtime.h>#include <linux/poll.h>#include <linux/security.h> #include "internal.h" struct poll_table_struct; static ssize_t default_read_file(struct file *file, char __user *buf,				 size_t count, loff_t *ppos){	return 0;} static ssize_t default_write_file(struct file *file, const char __user *buf,				   size_t count, loff_t *ppos){	return count;} const struct file_operations debugfs_noop_file_operations = {	.read =		default_read_file,	.write =	default_write_file,	.open =		simple_open,	.llseek =	noop_llseek,}; #define F_DENTRY(filp) ((filp)->f_path.dentry) void *debugfs_get_aux(const struct file *file){	return DEBUGFS_I(file_inode(file))->aux;}EXPORT_SYMBOL_GPL(debugfs_get_aux); enum dbgfs_get_mode {	DBGFS_GET_ALREADY,	DBGFS_GET_REGULAR,	DBGFS_GET_SHORT,}; static int __debugfs_file_get(struct dentry *dentry, enum dbgfs_get_mode mode){	struct debugfs_fsdata *fsd;	void *d_fsd; 	/*	 * This could only happen if some debugfs user erroneously calls	 * debugfs_file_get() on a dentry that isn't even a file, let	 * them know about it.	 */	if (WARN_ON(!d_is_reg(dentry)))		return -EINVAL; 	d_fsd = READ_ONCE(dentry->d_fsdata);	if (d_fsd) {		fsd = d_fsd;	} else {		struct inode *inode = dentry->d_inode;		unsigned int methods = 0; 		if (WARN_ON(mode == DBGFS_GET_ALREADY))			return -EINVAL; 		fsd = kmalloc_obj(*fsd);		if (!fsd)			return -ENOMEM; 		if (mode == DBGFS_GET_SHORT) {			const struct debugfs_short_fops *ops;			ops = fsd->short_fops = DEBUGFS_I(inode)->short_fops;			if (ops->llseek)				methods |= HAS_LSEEK;			if (ops->read)				methods |= HAS_READ;			if (ops->write)				methods |= HAS_WRITE;			fsd->real_fops = NULL;		} else {			const struct file_operations *ops;			ops = fsd->real_fops = DEBUGFS_I(inode)->real_fops;			if (ops->llseek)				methods |= HAS_LSEEK;			if (ops->read)				methods |= HAS_READ;			if (ops->write)				methods |= HAS_WRITE;			if (ops->unlocked_ioctl)				methods |= HAS_IOCTL;			if (ops->poll)				methods |= HAS_POLL;			fsd->short_fops = NULL;		}		fsd->methods = methods;		refcount_set(&fsd->active_users, 1);		init_completion(&fsd->active_users_drained);		INIT_LIST_HEAD(&fsd->cancellations);		mutex_init(&fsd->cancellations_mtx); 		d_fsd = cmpxchg(&dentry->d_fsdata, NULL, fsd);		if (d_fsd) {			mutex_destroy(&fsd->cancellations_mtx);			kfree(fsd);			fsd = d_fsd;		}	} 	/*	 * In case of a successful cmpxchg() above, this check is	 * strictly necessary and must follow it, see the comment in	 * __debugfs_remove_file().	 * OTOH, if the cmpxchg() hasn't been executed or wasn't	 * successful, this serves the purpose of not starving	 * removers.	 */	if (d_unlinked(dentry))		return -EIO; 	if (!refcount_inc_not_zero(&fsd->active_users))		return -EIO; 	return 0;} /** * debugfs_file_get - mark the beginning of file data access * @dentry: the dentry object whose data is being accessed. * * Up to a matching call to debugfs_file_put(), any successive call * into the file removing functions debugfs_remove() and * debugfs_remove_recursive() will block. Since associated private * file data may only get freed after a successful return of any of * the removal functions, you may safely access it after a successful * call to debugfs_file_get() without worrying about lifetime issues. * * If -%EIO is returned, the file has already been removed and thus, * it is not safe to access any of its data. If, on the other hand, * it is allowed to access the file data, zero is returned. */int debugfs_file_get(struct dentry *dentry){	return __debugfs_file_get(dentry, DBGFS_GET_ALREADY);}EXPORT_SYMBOL_GPL(debugfs_file_get); /** * debugfs_file_put - mark the end of file data access * @dentry: the dentry object formerly passed to *          debugfs_file_get(). * * Allow any ongoing concurrent call into debugfs_remove() or * debugfs_remove_recursive() blocked by a former call to * debugfs_file_get() to proceed and return to its caller. */void debugfs_file_put(struct dentry *dentry){	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata); 	if (refcount_dec_and_test(&fsd->active_users))		complete(&fsd->active_users_drained);}EXPORT_SYMBOL_GPL(debugfs_file_put); /** * debugfs_enter_cancellation - enter a debugfs cancellation * @file: the file being accessed * @cancellation: the cancellation object, the cancel callback *	inside of it must be initialized * * When a debugfs file is removed it needs to wait for all active * operations to complete. However, the operation itself may need * to wait for hardware or completion of some asynchronous process * or similar. As such, it may need to be cancelled to avoid long * waits or even deadlocks. * * This function can be used inside a debugfs handler that may * need to be cancelled. As soon as this function is called, the * cancellation's 'cancel' callback may be called, at which point * the caller should proceed to call debugfs_leave_cancellation() * and leave the debugfs handler function as soon as possible. * Note that the 'cancel' callback is only ever called in the * context of some kind of debugfs_remove(). * * This function must be paired with debugfs_leave_cancellation(). */void debugfs_enter_cancellation(struct file *file,				struct debugfs_cancellation *cancellation){	struct debugfs_fsdata *fsd;	struct dentry *dentry = F_DENTRY(file); 	INIT_LIST_HEAD(&cancellation->list); 	if (WARN_ON(!d_is_reg(dentry)))		return; 	if (WARN_ON(!cancellation->cancel))		return; 	fsd = READ_ONCE(dentry->d_fsdata);	if (WARN_ON(!fsd))		return; 	mutex_lock(&fsd->cancellations_mtx);	list_add(&cancellation->list, &fsd->cancellations);	mutex_unlock(&fsd->cancellations_mtx); 	/* if we're already removing wake it up to cancel */	if (d_unlinked(dentry))		complete(&fsd->active_users_drained);}EXPORT_SYMBOL_GPL(debugfs_enter_cancellation); /** * debugfs_leave_cancellation - leave cancellation section * @file: the file being accessed * @cancellation: the cancellation previously registered with *	debugfs_enter_cancellation() * * See the documentation of debugfs_enter_cancellation(). */void debugfs_leave_cancellation(struct file *file,				struct debugfs_cancellation *cancellation){	struct debugfs_fsdata *fsd;	struct dentry *dentry = F_DENTRY(file); 	if (WARN_ON(!d_is_reg(dentry)))		return; 	fsd = READ_ONCE(dentry->d_fsdata);	if (WARN_ON(!fsd))		return; 	mutex_lock(&fsd->cancellations_mtx);	if (!list_empty(&cancellation->list))		list_del(&cancellation->list);	mutex_unlock(&fsd->cancellations_mtx);}EXPORT_SYMBOL_GPL(debugfs_leave_cancellation); /* * Only permit access to world-readable files when the kernel is locked down. * We also need to exclude any file that has ways to write or alter it as root * can bypass the permissions check. */static int debugfs_locked_down(struct inode *inode,			       struct file *filp,			       const struct file_operations *real_fops){	if ((inode->i_mode & 07777 & ~0444) == 0 &&	    !(filp->f_mode & FMODE_WRITE) &&	    (!real_fops ||	     (!real_fops->unlocked_ioctl &&	      !real_fops->compat_ioctl &&	      !real_fops->mmap)))		return 0; 	if (security_locked_down(LOCKDOWN_DEBUGFS))		return -EPERM; 	return 0;} static int open_proxy_open(struct inode *inode, struct file *filp){	struct dentry *dentry = F_DENTRY(filp);	const struct file_operations *real_fops = DEBUGFS_I(inode)->real_fops;	int r; 	r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);	if (r)		return r == -EIO ? -ENOENT : r; 	r = debugfs_locked_down(inode, filp, real_fops);	if (r)		goto out; 	if (!fops_get(real_fops)) {#ifdef CONFIG_MODULES		if (real_fops->owner &&		    real_fops->owner->state == MODULE_STATE_GOING) {			r = -ENXIO;			goto out;		}#endif 		/* Huh? Module did not clean up after itself at exit? */		WARN(1, "debugfs file owner did not clean up at exit: %pd",			dentry);		r = -ENXIO;		goto out;	}	replace_fops(filp, real_fops); 	if (real_fops->open)		r = real_fops->open(inode, filp); out:	debugfs_file_put(dentry);	return r;} const struct file_operations debugfs_open_proxy_file_operations = {	.open = open_proxy_open,}; #define PROTO(args...) args#define ARGS(args...) args #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret)	\static ret_type full_proxy_ ## name(proto)				\{									\	struct dentry *dentry = F_DENTRY(filp);				\	struct debugfs_fsdata *fsd = dentry->d_fsdata;			\	ret_type r;							\									\	if (!(fsd->methods & bit))					\		return ret;						\	r = debugfs_file_get(dentry);					\	if (unlikely(r))						\		return r;						\	r = fsd->real_fops->name(args);					\	debugfs_file_put(dentry);					\	return r;							\} #define SHORT_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret)	\static ret_type short_proxy_ ## name(proto)				\{									\	struct dentry *dentry = F_DENTRY(filp);				\	struct debugfs_fsdata *fsd = dentry->d_fsdata;			\	ret_type r;							\									\	if (!(fsd->methods & bit))					\		return ret;						\	r = debugfs_file_get(dentry);					\	if (unlikely(r))						\		return r;						\	r = fsd->short_fops->name(args);				\	debugfs_file_put(dentry);					\	return r;							\} SHORT_PROXY_FUNC(llseek, loff_t, filp,		PROTO(struct file *filp, loff_t offset, int whence),		ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE); FULL_PROXY_FUNC(llseek, loff_t, filp,		PROTO(struct file *filp, loff_t offset, int whence),		ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE); SHORT_PROXY_FUNC(read, ssize_t, filp,		PROTO(struct file *filp, char __user *buf, size_t size,			loff_t *ppos),		ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL); FULL_PROXY_FUNC(read, ssize_t, filp,		PROTO(struct file *filp, char __user *buf, size_t size,			loff_t *ppos),		ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL); SHORT_PROXY_FUNC(write, ssize_t, filp,		PROTO(struct file *filp, const char __user *buf,			size_t size, loff_t *ppos),		ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL); FULL_PROXY_FUNC(write, ssize_t, filp,		PROTO(struct file *filp, const char __user *buf,			size_t size, loff_t *ppos),		ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL); FULL_PROXY_FUNC(unlocked_ioctl, long, filp,		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),		ARGS(filp, cmd, arg), HAS_IOCTL, -ENOTTY); static __poll_t full_proxy_poll(struct file *filp,				struct poll_table_struct *wait){	struct dentry *dentry = F_DENTRY(filp);	struct debugfs_fsdata *fsd = dentry->d_fsdata;	__poll_t r = 0; 	if (!(fsd->methods & HAS_POLL))		return DEFAULT_POLLMASK;	if (debugfs_file_get(dentry))		return EPOLLHUP; 	r = fsd->real_fops->poll(filp, wait);	debugfs_file_put(dentry);	return r;} static int full_proxy_release(struct inode *inode, struct file *file){	struct debugfs_fsdata *fsd = F_DENTRY(file)->d_fsdata;	const struct file_operations *real_fops = fsd->real_fops;	int r = 0; 	/*	 * We must not protect this against removal races here: the	 * original releaser should be called unconditionally in order	 * not to leak any resources. Releasers must not assume that	 * ->i_private is still being meaningful here.	 */	if (real_fops->release)		r = real_fops->release(inode, file); 	fops_put(real_fops);	return r;} static int full_proxy_open_regular(struct inode *inode, struct file *filp){	struct dentry *dentry = F_DENTRY(filp);	const struct file_operations *real_fops;	struct debugfs_fsdata *fsd;	int r; 	r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);	if (r)		return r == -EIO ? -ENOENT : r; 	fsd = dentry->d_fsdata;	real_fops = fsd->real_fops;	r = debugfs_locked_down(inode, filp, real_fops);	if (r)		goto out; 	if (!fops_get(real_fops)) {#ifdef CONFIG_MODULES		if (real_fops->owner &&		    real_fops->owner->state == MODULE_STATE_GOING) {			r = -ENXIO;			goto out;		}#endif 		/* Huh? Module did not cleanup after itself at exit? */		WARN(1, "debugfs file owner did not clean up at exit: %pd",			dentry);		r = -ENXIO;		goto out;	} 	if (real_fops->open) {		r = real_fops->open(inode, filp);		if (r) {			fops_put(real_fops);		} else if (filp->f_op != &debugfs_full_proxy_file_operations) {			/* No protection against file removal anymore. */			WARN(1, "debugfs file owner replaced proxy fops: %pd",				dentry);			fops_put(real_fops);		}	}out:	debugfs_file_put(dentry);	return r;} const struct file_operations debugfs_full_proxy_file_operations = {	.open = full_proxy_open_regular,	.release = full_proxy_release,	.llseek = full_proxy_llseek,	.read = full_proxy_read,	.write = full_proxy_write,	.poll = full_proxy_poll,	.unlocked_ioctl = full_proxy_unlocked_ioctl}; static int full_proxy_open_short(struct inode *inode, struct file *filp){	struct dentry *dentry = F_DENTRY(filp);	int r; 	r = __debugfs_file_get(dentry, DBGFS_GET_SHORT);	if (r)		return r == -EIO ? -ENOENT : r;	r = debugfs_locked_down(inode, filp, NULL);

Lines 1–500 of 1435.