Merge "ANDROID: ion: Protect kref from userspace manipulation"
This commit is contained in:
commit
9cf7045fb6
1 changed files with 78 additions and 7 deletions
|
@ -114,6 +114,7 @@ struct ion_client {
|
||||||
*/
|
*/
|
||||||
struct ion_handle {
|
struct ion_handle {
|
||||||
struct kref ref;
|
struct kref ref;
|
||||||
|
unsigned int user_ref_count;
|
||||||
struct ion_client *client;
|
struct ion_client *client;
|
||||||
struct ion_buffer *buffer;
|
struct ion_buffer *buffer;
|
||||||
struct rb_node node;
|
struct rb_node node;
|
||||||
|
@ -433,6 +434,50 @@ int ion_handle_put(struct ion_handle *handle)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Must hold the client lock */
|
||||||
|
static void user_ion_handle_get(struct ion_handle *handle)
|
||||||
|
{
|
||||||
|
if (handle->user_ref_count++ == 0)
|
||||||
|
kref_get(&handle->ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Must hold the client lock */
|
||||||
|
static struct ion_handle *user_ion_handle_get_check_overflow(
|
||||||
|
struct ion_handle *handle)
|
||||||
|
{
|
||||||
|
if (handle->user_ref_count + 1 == 0)
|
||||||
|
return ERR_PTR(-EOVERFLOW);
|
||||||
|
user_ion_handle_get(handle);
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* passes a kref to the user ref count.
|
||||||
|
* We know we're holding a kref to the object before and
|
||||||
|
* after this call, so no need to reverify handle.
|
||||||
|
*/
|
||||||
|
static struct ion_handle *pass_to_user(struct ion_handle *handle)
|
||||||
|
{
|
||||||
|
struct ion_client *client = handle->client;
|
||||||
|
struct ion_handle *ret;
|
||||||
|
|
||||||
|
mutex_lock(&client->lock);
|
||||||
|
ret = user_ion_handle_get_check_overflow(handle);
|
||||||
|
ion_handle_put_nolock(handle);
|
||||||
|
mutex_unlock(&client->lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Must hold the client lock */
|
||||||
|
static int user_ion_handle_put_nolock(struct ion_handle *handle)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (--handle->user_ref_count == 0)
|
||||||
|
ret = ion_handle_put_nolock(handle);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
static struct ion_handle *ion_handle_lookup(struct ion_client *client,
|
static struct ion_handle *ion_handle_lookup(struct ion_client *client,
|
||||||
struct ion_buffer *buffer)
|
struct ion_buffer *buffer)
|
||||||
{
|
{
|
||||||
|
@ -644,6 +689,25 @@ static void ion_free_nolock(struct ion_client *client, struct ion_handle *handle
|
||||||
ion_handle_put_nolock(handle);
|
ion_handle_put_nolock(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void user_ion_free_nolock(struct ion_client *client,
|
||||||
|
struct ion_handle *handle)
|
||||||
|
{
|
||||||
|
bool valid_handle;
|
||||||
|
|
||||||
|
WARN_ON(client != handle->client);
|
||||||
|
|
||||||
|
valid_handle = ion_handle_validate(client, handle);
|
||||||
|
if (!valid_handle) {
|
||||||
|
WARN(1, "%s: invalid handle passed to free.\n", __func__);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!handle->user_ref_count > 0) {
|
||||||
|
WARN(1, "%s: User does not have access!\n", __func__);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
user_ion_handle_put_nolock(handle);
|
||||||
|
}
|
||||||
|
|
||||||
void ion_free(struct ion_client *client, struct ion_handle *handle)
|
void ion_free(struct ion_client *client, struct ion_handle *handle)
|
||||||
{
|
{
|
||||||
BUG_ON(client != handle->client);
|
BUG_ON(client != handle->client);
|
||||||
|
@ -1518,7 +1582,7 @@ static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
||||||
data.allocation.flags, true);
|
data.allocation.flags, true);
|
||||||
if (IS_ERR(handle))
|
if (IS_ERR(handle))
|
||||||
return PTR_ERR(handle);
|
return PTR_ERR(handle);
|
||||||
|
pass_to_user(handle);
|
||||||
data.allocation.handle = handle->id;
|
data.allocation.handle = handle->id;
|
||||||
|
|
||||||
cleanup_handle = handle;
|
cleanup_handle = handle;
|
||||||
|
@ -1534,7 +1598,7 @@ static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
||||||
mutex_unlock(&client->lock);
|
mutex_unlock(&client->lock);
|
||||||
return PTR_ERR(handle);
|
return PTR_ERR(handle);
|
||||||
}
|
}
|
||||||
ion_free_nolock(client, handle);
|
user_ion_free_nolock(client, handle);
|
||||||
ion_handle_put_nolock(handle);
|
ion_handle_put_nolock(handle);
|
||||||
mutex_unlock(&client->lock);
|
mutex_unlock(&client->lock);
|
||||||
break;
|
break;
|
||||||
|
@ -1558,10 +1622,15 @@ static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
||||||
struct ion_handle *handle;
|
struct ion_handle *handle;
|
||||||
|
|
||||||
handle = ion_import_dma_buf(client, data.fd.fd);
|
handle = ion_import_dma_buf(client, data.fd.fd);
|
||||||
if (IS_ERR(handle))
|
if (IS_ERR(handle)) {
|
||||||
ret = PTR_ERR(handle);
|
ret = PTR_ERR(handle);
|
||||||
else
|
} else {
|
||||||
data.handle.handle = handle->id;
|
handle = pass_to_user(handle);
|
||||||
|
if (IS_ERR(handle))
|
||||||
|
ret = PTR_ERR(handle);
|
||||||
|
else
|
||||||
|
data.handle.handle = handle->id;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ION_IOC_SYNC:
|
case ION_IOC_SYNC:
|
||||||
|
@ -1593,8 +1662,10 @@ static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
||||||
if (dir & _IOC_READ) {
|
if (dir & _IOC_READ) {
|
||||||
if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd))) {
|
if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd))) {
|
||||||
if (cleanup_handle) {
|
if (cleanup_handle) {
|
||||||
ion_free(client, cleanup_handle);
|
mutex_lock(&client->lock);
|
||||||
ion_handle_put(cleanup_handle);
|
user_ion_free_nolock(client, cleanup_handle);
|
||||||
|
ion_handle_put_nolock(cleanup_handle);
|
||||||
|
mutex_unlock(&client->lock);
|
||||||
}
|
}
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue