forked from mirrors/linux
		
	[NET]: Fix race when opening a proc file while a network namespace is exiting.
The problem: proc_net files remember which network namespace the are against but do not remember hold a reference count (as that would pin the network namespace). So we currently have a small window where the reference count on a network namespace may be incremented when opening a /proc file when it has already gone to zero. To fix this introduce maybe_get_net and get_proc_net. maybe_get_net increments the network namespace reference count only if it is greater then zero, ensuring we don't increment a reference count after it has gone to zero. get_proc_net handles all of the magic to go from a proc inode to the network namespace instance and call maybe_get_net on it. PROC_NET the old accessor is removed so that we don't get confused and use the wrong helper function. Then I fix up the callers to use get_proc_net and handle the case case where get_proc_net returns NULL. In that case I return -ENXIO because effectively the network namespace has already gone away so the files we are trying to access don't exist anymore. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Acked-by: Paul E. McKenney <paulmck@us.ibm.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
		
							parent
							
								
									4fabcd7118
								
							
						
					
					
						commit
						077130c0cf
					
				
					 7 changed files with 39 additions and 8 deletions
				
			
		| 
						 | 
					@ -51,6 +51,12 @@ void proc_net_remove(struct net *net, const char *name)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
EXPORT_SYMBOL_GPL(proc_net_remove);
 | 
					EXPORT_SYMBOL_GPL(proc_net_remove);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct net *get_proc_net(const struct inode *inode)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return maybe_get_net(PDE_NET(PDE(inode)));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					EXPORT_SYMBOL_GPL(get_proc_net);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct proc_dir_entry *proc_net_shadow;
 | 
					static struct proc_dir_entry *proc_net_shadow;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct dentry *proc_net_shadow_dentry(struct dentry *parent,
 | 
					static struct dentry *proc_net_shadow_dentry(struct dentry *parent,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -270,10 +270,7 @@ static inline struct net *PDE_NET(struct proc_dir_entry *pde)
 | 
				
			||||||
	return pde->parent->data;
 | 
						return pde->parent->data;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline struct net *PROC_NET(const struct inode *inode)
 | 
					struct net *get_proc_net(const struct inode *inode);
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	return PDE_NET(PDE(inode));
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct proc_maps_private {
 | 
					struct proc_maps_private {
 | 
				
			||||||
	struct pid *pid;
 | 
						struct pid *pid;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -46,6 +46,18 @@ static inline struct net *get_net(struct net *net)
 | 
				
			||||||
	return net;
 | 
						return net;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static inline struct net *maybe_get_net(struct net *net)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						/* Used when we know struct net exists but we
 | 
				
			||||||
 | 
						 * aren't guaranteed a previous reference count
 | 
				
			||||||
 | 
						 * exists.  If the reference count is zero this
 | 
				
			||||||
 | 
						 * function fails and returns NULL.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						if (!atomic_inc_not_zero(&net->count))
 | 
				
			||||||
 | 
							net = NULL;
 | 
				
			||||||
 | 
						return net;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline void put_net(struct net *net)
 | 
					static inline void put_net(struct net *net)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (atomic_dec_and_test(&net->count))
 | 
						if (atomic_dec_and_test(&net->count))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2464,7 +2464,11 @@ static int dev_seq_open(struct inode *inode, struct file *file)
 | 
				
			||||||
	res =  seq_open(file, &dev_seq_ops);
 | 
						res =  seq_open(file, &dev_seq_ops);
 | 
				
			||||||
	if (!res) {
 | 
						if (!res) {
 | 
				
			||||||
		seq = file->private_data;
 | 
							seq = file->private_data;
 | 
				
			||||||
		seq->private = get_net(PROC_NET(inode));
 | 
							seq->private = get_proc_net(inode);
 | 
				
			||||||
 | 
							if (!seq->private) {
 | 
				
			||||||
 | 
								seq_release(inode, file);
 | 
				
			||||||
 | 
								res = -ENXIO;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return res;
 | 
						return res;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -246,7 +246,11 @@ static int dev_mc_seq_open(struct inode *inode, struct file *file)
 | 
				
			||||||
	res = seq_open(file, &dev_mc_seq_ops);
 | 
						res = seq_open(file, &dev_mc_seq_ops);
 | 
				
			||||||
	if (!res) {
 | 
						if (!res) {
 | 
				
			||||||
		seq = file->private_data;
 | 
							seq = file->private_data;
 | 
				
			||||||
		seq->private = get_net(PROC_NET(inode));
 | 
							seq->private = get_proc_net(inode);
 | 
				
			||||||
 | 
							if (!seq->private) {
 | 
				
			||||||
 | 
								seq_release(inode, file);
 | 
				
			||||||
 | 
								res = -ENXIO;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return res;
 | 
						return res;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1859,7 +1859,11 @@ static int netlink_seq_open(struct inode *inode, struct file *file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	seq = file->private_data;
 | 
						seq = file->private_data;
 | 
				
			||||||
	seq->private = iter;
 | 
						seq->private = iter;
 | 
				
			||||||
	iter->net = get_net(PROC_NET(inode));
 | 
						iter->net = get_proc_net(inode);
 | 
				
			||||||
 | 
						if (!iter->net) {
 | 
				
			||||||
 | 
							seq_release_private(inode, file);
 | 
				
			||||||
 | 
							return -ENXIO;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -678,7 +678,11 @@ static int wireless_seq_open(struct inode *inode, struct file *file)
 | 
				
			||||||
	res = seq_open(file, &wireless_seq_ops);
 | 
						res = seq_open(file, &wireless_seq_ops);
 | 
				
			||||||
	if (!res) {
 | 
						if (!res) {
 | 
				
			||||||
		seq = file->private_data;
 | 
							seq = file->private_data;
 | 
				
			||||||
		seq->private = get_net(PROC_NET(inode));
 | 
							seq->private = get_proc_net(inode);
 | 
				
			||||||
 | 
							if (!seq->private) {
 | 
				
			||||||
 | 
								seq_release(inode, file);
 | 
				
			||||||
 | 
								res = -ENXIO;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return res;
 | 
						return res;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue