Blame kmod/core/shadow.c

Packit Service ac8aad
/*
Packit Service ac8aad
 * Copyright (C) 2014 Josh Poimboeuf <jpoimboe@redhat.com>
Packit Service ac8aad
 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
Packit Service ac8aad
 *
Packit Service ac8aad
 * This program is free software; you can redistribute it and/or
Packit Service ac8aad
 * modify it under the terms of the GNU General Public License
Packit Service ac8aad
 * as published by the Free Software Foundation; either version 2
Packit Service ac8aad
 * of the License, or (at your option) any later version.
Packit Service ac8aad
 *
Packit Service ac8aad
 * This program is distributed in the hope that it will be useful,
Packit Service ac8aad
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service ac8aad
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service ac8aad
 * GNU General Public License for more details.
Packit Service ac8aad
 *
Packit Service ac8aad
 * You should have received a copy of the GNU General Public License
Packit Service ac8aad
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit Service ac8aad
 */
Packit Service ac8aad
Packit Service ac8aad
/*
Packit Service ac8aad
 * kpatch shadow variables
Packit Service ac8aad
 *
Packit Service ac8aad
 * These functions can be used to add new "shadow" fields to existing data
Packit Service ac8aad
 * structures.  For example, to allocate a "newpid" variable associated with an
Packit Service ac8aad
 * instance of task_struct, and assign it a value of 1000:
Packit Service ac8aad
 *
Packit Service ac8aad
 * struct task_struct *tsk = current;
Packit Service ac8aad
 * int *newpid;
Packit Service ac8aad
 * newpid = kpatch_shadow_alloc(tsk, "newpid", sizeof(int), GFP_KERNEL);
Packit Service ac8aad
 * if (newpid)
Packit Service ac8aad
 * 	*newpid = 1000;
Packit Service ac8aad
 *
Packit Service ac8aad
 * To retrieve a pointer to the variable:
Packit Service ac8aad
 *
Packit Service ac8aad
 * struct task_struct *tsk = current;
Packit Service ac8aad
 * int *newpid;
Packit Service ac8aad
 * newpid = kpatch_shadow_get(tsk, "newpid");
Packit Service ac8aad
 * if (newpid)
Packit Service ac8aad
 * 	printk("task newpid = %d\n", *newpid); // prints "task newpid = 1000"
Packit Service ac8aad
 *
Packit Service ac8aad
 * To free it:
Packit Service ac8aad
 *
Packit Service ac8aad
 * kpatch_shadow_free(tsk, "newpid");
Packit Service ac8aad
 */
Packit Service ac8aad
Packit Service ac8aad
#include <linux/hashtable.h>
Packit Service ac8aad
#include <linux/slab.h>
Packit Service ac8aad
#include "kpatch.h"
Packit Service ac8aad
Packit Service ac8aad
static DEFINE_HASHTABLE(kpatch_shadow_hash, 12);
Packit Service ac8aad
static DEFINE_SPINLOCK(kpatch_shadow_lock);
Packit Service ac8aad
Packit Service ac8aad
struct kpatch_shadow {
Packit Service ac8aad
	struct hlist_node node;
Packit Service ac8aad
	struct rcu_head rcu_head;
Packit Service ac8aad
	void *obj;
Packit Service ac8aad
	union {
Packit Service ac8aad
		char *var; /* assumed to be 4-byte aligned */
Packit Service ac8aad
		unsigned long flags;
Packit Service ac8aad
	};
Packit Service ac8aad
	void *data;
Packit Service ac8aad
};
Packit Service ac8aad
Packit Service ac8aad
#define SHADOW_FLAG_INPLACE 0x1
Packit Service ac8aad
#define SHADOW_FLAG_RESERVED0 0x2 /* reserved for future use */
Packit Service ac8aad
Packit Service ac8aad
#define SHADOW_FLAG_MASK 0x3
Packit Service ac8aad
#define SHADOW_PTR_MASK (~(SHADOW_FLAG_MASK))
Packit Service ac8aad
Packit Service ac8aad
static inline void shadow_set_inplace(struct kpatch_shadow *shadow)
Packit Service ac8aad
{
Packit Service ac8aad
	shadow->flags |= SHADOW_FLAG_INPLACE;
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static inline int shadow_is_inplace(struct kpatch_shadow *shadow)
Packit Service ac8aad
{
Packit Service ac8aad
	return shadow->flags & SHADOW_FLAG_INPLACE;
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static inline char *shadow_var(struct kpatch_shadow *shadow)
Packit Service ac8aad
{
Packit Service ac8aad
	return (char *)((unsigned long)shadow->var & SHADOW_PTR_MASK);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
void *kpatch_shadow_alloc(void *obj, char *var, size_t size, gfp_t gfp)
Packit Service ac8aad
{
Packit Service ac8aad
	unsigned long flags;
Packit Service ac8aad
	struct kpatch_shadow *shadow;
Packit Service ac8aad
Packit Service ac8aad
	shadow = kmalloc(sizeof(*shadow), gfp);
Packit Service ac8aad
	if (!shadow)
Packit Service ac8aad
		return NULL;
Packit Service ac8aad
Packit Service ac8aad
	shadow->obj = obj;
Packit Service ac8aad
Packit Service ac8aad
	shadow->var = kstrdup(var, gfp);
Packit Service ac8aad
	if (!shadow->var) {
Packit Service ac8aad
		kfree(shadow);
Packit Service ac8aad
		return NULL;
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	if (size <= sizeof(shadow->data)) {
Packit Service ac8aad
		shadow->data = &shadow->data;
Packit Service ac8aad
		shadow_set_inplace(shadow);
Packit Service ac8aad
	} else {
Packit Service ac8aad
		shadow->data = kmalloc(size, gfp);
Packit Service ac8aad
		if (!shadow->data) {
Packit Service ac8aad
			kfree(shadow->var);
Packit Service ac8aad
			kfree(shadow);
Packit Service ac8aad
			return NULL;
Packit Service ac8aad
		}
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	spin_lock_irqsave(&kpatch_shadow_lock, flags);
Packit Service ac8aad
	hash_add_rcu(kpatch_shadow_hash, &shadow->node, (unsigned long)obj);
Packit Service ac8aad
	spin_unlock_irqrestore(&kpatch_shadow_lock, flags);
Packit Service ac8aad
Packit Service ac8aad
	return shadow->data;
Packit Service ac8aad
}
Packit Service ac8aad
EXPORT_SYMBOL_GPL(kpatch_shadow_alloc);
Packit Service ac8aad
Packit Service ac8aad
static void kpatch_shadow_rcu_free(struct rcu_head *head)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_shadow *shadow;
Packit Service ac8aad
Packit Service ac8aad
	shadow = container_of(head, struct kpatch_shadow, rcu_head);
Packit Service ac8aad
Packit Service ac8aad
	if (!shadow_is_inplace(shadow))
Packit Service ac8aad
		kfree(shadow->data);
Packit Service ac8aad
	kfree(shadow_var(shadow));
Packit Service ac8aad
	kfree(shadow);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
void kpatch_shadow_free(void *obj, char *var)
Packit Service ac8aad
{
Packit Service ac8aad
	unsigned long flags;
Packit Service ac8aad
	struct kpatch_shadow *shadow;
Packit Service ac8aad
Packit Service ac8aad
	spin_lock_irqsave(&kpatch_shadow_lock, flags);
Packit Service ac8aad
Packit Service ac8aad
	hash_for_each_possible(kpatch_shadow_hash, shadow, node,
Packit Service ac8aad
			       (unsigned long)obj) {
Packit Service ac8aad
		if (shadow->obj == obj && !strcmp(shadow_var(shadow), var)) {
Packit Service ac8aad
			hash_del_rcu(&shadow->node);
Packit Service ac8aad
			spin_unlock_irqrestore(&kpatch_shadow_lock, flags);
Packit Service ac8aad
			call_rcu(&shadow->rcu_head, kpatch_shadow_rcu_free);
Packit Service ac8aad
			return;
Packit Service ac8aad
		}
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	spin_unlock_irqrestore(&kpatch_shadow_lock, flags);
Packit Service ac8aad
}
Packit Service ac8aad
EXPORT_SYMBOL_GPL(kpatch_shadow_free);
Packit Service ac8aad
Packit Service ac8aad
void *kpatch_shadow_get(void *obj, char *var)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_shadow *shadow;
Packit Service ac8aad
Packit Service ac8aad
	rcu_read_lock();
Packit Service ac8aad
Packit Service ac8aad
	hash_for_each_possible_rcu(kpatch_shadow_hash, shadow, node,
Packit Service ac8aad
				   (unsigned long)obj) {
Packit Service ac8aad
		if (shadow->obj == obj && !strcmp(shadow_var(shadow), var)) {
Packit Service ac8aad
			rcu_read_unlock();
Packit Service ac8aad
			if (shadow_is_inplace(shadow))
Packit Service ac8aad
				return &(shadow->data);
Packit Service ac8aad
Packit Service ac8aad
			return shadow->data;
Packit Service ac8aad
		}
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	rcu_read_unlock();
Packit Service ac8aad
Packit Service ac8aad
	return NULL;
Packit Service ac8aad
}
Packit Service ac8aad
EXPORT_SYMBOL_GPL(kpatch_shadow_get);