Skip to contentSkip to conversation

irqchip/gicv3-its: Don't allow devices whose ID is outside range

PATCH Superseded Shanker Donthineni 3 participants 5 messages Feb 28, 2016 – Mar 10, 2016

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 irqchip/gicv3-its: Don't allow devices whose ID is outside range

We are not checking whether the requested device identifier fits into the device table memory or not. The function its_create_device() assumes that enough memory has been allocated for whole DevID sparse (reported by ITS_TYPER.Devbits) during the ITS probe() and continues to initialize ITS hardware.

This assumption is not perfect, sometimes we reduce memory size either because of its size crossing MAX_ORDER-1 or BASERn max size limit. The MAPD command fails if 'Device ID' is outside of device table range.

Add a simple validation check to avoid MAPD failures since we are not handling ITS command errors. This change also helps to return an error -ENOMEM instead of success to caller.

---
[v1]->[v2]
Rebase to v4.5-rc6, edit commit text and simplify code changes

 drivers/irqchip/irq-gic-v3-its.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +20 -4
index 43dfd15..6d986ac 100644
@@ -55,6 +55,16 @@ struct its_collection {
 };
 
 /*
+ * The ITS_BASER structure - contains memeory information and table
+ * entry size in bytes.
+ */
+struct its_baser {
+	void		*base;
+	u32		order;
+	u32		entry_size;
+};
+
+/*
  * The ITS structure - contains most of the infrastructure, with the
  * top-level MSI domain, the command queue, the collections, and the
  * list of devices writing to it.
@@ -66,14 +76,12 @@ struct its_node {
 	unsigned long		phys_base;
 	struct its_cmd_block	*cmd_base;
 	struct its_cmd_block	*cmd_write;
-	struct {
-		void		*base;
-		u32		order;
-	} tables[GITS_BASER_NR_REGS];
+	struct its_baser	tables[GITS_BASER_NR_REGS];
 	struct its_collection	*collections;
 	struct list_head	its_device_list;
 	u64			flags;
 	u32			ite_size;
+	struct its_baser	*device_table;
 };
 
 #define ITS_ITT_ALIGN		SZ_256
@@ -860,6 +868,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
 		 * For other tables, only allocate a single page.
 		 */
 		if (type == GITS_BASER_TYPE_DEVICE) {
+			its->device_table = &its->tables[i];
 			/*
 			 * 'order' was initialized earlier to the default page
 			 * granule of the the ITS.  We can't have an allocation
@@ -874,6 +883,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
 					node_name, order);
 			}
 		}
+		its->tables[i].entry_size = entry_size;
 
 retry_alloc_baser:
 		alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
@@ -1152,6 +1162,12 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
 	int nr_ites;
 	int sz;
 
+	/* Don't allow 'dev_id' that exceeds single, flat table limit */
+	if (its->device_table &&
+	    (dev_id >= (PAGE_ORDER_TO_SIZE(its->device_table->order) /
+	    its->device_table->entry_size)))
+		return NULL;
+
 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 	/*
 	 * At least one bit of EventID is being used, hence a minimum
Signature · 3 lines
Qualcomm Technologies, Inc. on behalf of Qualcomm Innovation Center, Inc. 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, 
a Linux Foundation Collaborative Project
Reviewed-by on v2 1/1
#1 lore raw

Hi Shanker,

Quoted 3 lines · Shanker Donthineni

We are not checking whether the requested device identifier fits into the device table memory or not. The function its_create_device() assumes that enough memory has been allocated for whole DevID sparse

space?

Quoted 24 lines

(reported by ITS_TYPER.Devbits) during the ITS probe() and continues to initialize ITS hardware.

This assumption is not perfect, sometimes we reduce memory size either because of its size crossing MAX_ORDER-1 or BASERn max size limit. The MAPD command fails if 'Device ID' is outside of device table range.

Add a simple validation check to avoid MAPD failures since we are not handling ITS command errors. This change also helps to return an error -ENOMEM instead of success to caller.

---
[v1]->[v2]
Rebase to v4.5-rc6, edit commit text and simplify code changes

 drivers/irqchip/irq-gic-v3-its.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +0 -0
index 43dfd15..6d986ac 100644

drivers/irqchip/irq-gic-v3-its.c struct its_collection { · line 58

Quoted 4 lines
drivers/irqchip/irq-gic-v3-its.c +1 -0
@@ -55,6 +55,16 @@ struct its_collection {
 };
 
 /*
+ * The ITS_BASER structure - contains memeory information and table

typo: memory

Best Regards

Eric

Quoted 59 lines
drivers/irqchip/irq-gic-v3-its.c +19 -4
+ * entry size in bytes.
+ */
+struct its_baser {
+	void		*base;
+	u32		order;
+	u32		entry_size;
+};
+
+/*
  * The ITS structure - contains most of the infrastructure, with the
  * top-level MSI domain, the command queue, the collections, and the
  * list of devices writing to it.
@@ -66,14 +76,12 @@ struct its_node {
 	unsigned long		phys_base;
 	struct its_cmd_block	*cmd_base;
 	struct its_cmd_block	*cmd_write;
-	struct {
-		void		*base;
-		u32		order;
-	} tables[GITS_BASER_NR_REGS];
+	struct its_baser	tables[GITS_BASER_NR_REGS];
 	struct its_collection	*collections;
 	struct list_head	its_device_list;
 	u64			flags;
 	u32			ite_size;
+	struct its_baser	*device_table;
 };
 
 #define ITS_ITT_ALIGN		SZ_256
@@ -860,6 +868,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
 		 * For other tables, only allocate a single page.
 		 */
 		if (type == GITS_BASER_TYPE_DEVICE) {
+			its->device_table = &its->tables[i];
 			/*
 			 * 'order' was initialized earlier to the default page
 			 * granule of the the ITS.  We can't have an allocation
@@ -874,6 +883,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
 					node_name, order);
 			}
 		}
+		its->tables[i].entry_size = entry_size;
 
 retry_alloc_baser:
 		alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
@@ -1152,6 +1162,12 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
 	int nr_ites;
 	int sz;
 
+	/* Don't allow 'dev_id' that exceeds single, flat table limit */
+	if (its->device_table &&
+	    (dev_id >= (PAGE_ORDER_TO_SIZE(its->device_table->order) /
+	    its->device_table->entry_size)))
+		return NULL;
+
 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 	/*
 	 * At least one bit of EventID is being used, hence a minimum
#2 lore raw

Hi Eric,

Quoted 6 lines · Eric Auger

Hi Shanker,

Quoted 3 lines · Shanker Donthineni

We are not checking whether the requested device identifier fits into the device table memory or not. The function its_create_device() assumes that enough memory has been allocated for whole DevID sparse

space?

Sure, I will change to space.

Quoted 26 lines
Quoted 26 lines

(reported by ITS_TYPER.Devbits) during the ITS probe() and continues to initialize ITS hardware.

This assumption is not perfect, sometimes we reduce memory size either because of its size crossing MAX_ORDER-1 or BASERn max size limit. The MAPD command fails if 'Device ID' is outside of device table range.

Add a simple validation check to avoid MAPD failures since we are not handling ITS command errors. This change also helps to return an error -ENOMEM instead of success to caller.

---
[v1]->[v2]
Rebase to v4.5-rc6, edit commit text and simplify code changes

  drivers/irqchip/irq-gic-v3-its.c | 24 ++++++++++++++++++++----
  1 file changed, 20 insertions(+), 4 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +0 -0
index 43dfd15..6d986ac 100644
@@ -55,6 +55,16 @@ struct its_collection {
  };
 
Quoted 3 lines

drivers/irqchip/irq-gic-v3-its.c struct its_collection { · line 58

Quoted 2 lines
drivers/irqchip/irq-gic-v3-its.c +1 -0
  /*
+ * The ITS_BASER structure - contains memeory information and table

typo: memory

Thanks, I will fix in next patch.

Quoted 64 lines

Best Regards

Eric

Quoted 59 lines
drivers/irqchip/irq-gic-v3-its.c +19 -4
+ * entry size in bytes.
+ */
+struct its_baser {
+	void		*base;
+	u32		order;
+	u32		entry_size;
+};
+
+/*
   * The ITS structure - contains most of the infrastructure, with the
   * top-level MSI domain, the command queue, the collections, and the
   * list of devices writing to it.
@@ -66,14 +76,12 @@ struct its_node {
  	unsigned long		phys_base;
  	struct its_cmd_block	*cmd_base;
  	struct its_cmd_block	*cmd_write;
-	struct {
-		void		*base;
-		u32		order;
-	} tables[GITS_BASER_NR_REGS];
+	struct its_baser	tables[GITS_BASER_NR_REGS];
  	struct its_collection	*collections;
  	struct list_head	its_device_list;
  	u64			flags;
  	u32			ite_size;
+	struct its_baser	*device_table;
  };
 
  #define ITS_ITT_ALIGN		SZ_256
@@ -860,6 +868,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
  		 * For other tables, only allocate a single page.
  		 */
  		if (type == GITS_BASER_TYPE_DEVICE) {
+			its->device_table = &its->tables[i];
  			/*
  			 * 'order' was initialized earlier to the default page
  			 * granule of the the ITS.  We can't have an allocation
@@ -874,6 +883,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
  					node_name, order);
  			}
  		}
+		its->tables[i].entry_size = entry_size;
 
  retry_alloc_baser:
  		alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
@@ -1152,6 +1162,12 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
  	int nr_ites;
  	int sz;
 
+	/* Don't allow 'dev_id' that exceeds single, flat table limit */
+	if (its->device_table &&
+	    (dev_id >= (PAGE_ORDER_TO_SIZE(its->device_table->order) /
+	    its->device_table->entry_size)))
+		return NULL;
+
  	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  	/*
  	 * At least one bit of EventID is being used, hence a minimum
Signature · 3 lines
Shanker Donthineni
Qualcomm Technologies, Inc. on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
#3 lore raw
In reply to Shanker Donthineni

On Sun, 28 Feb 2016 13:22:03 -0600
Shanker Donthineni <[email protected]> wrote:

Hi Shanker,

Quoted 47 lines

We are not checking whether the requested device identifier fits into the device table memory or not. The function its_create_device() assumes that enough memory has been allocated for whole DevID sparse (reported by ITS_TYPER.Devbits) during the ITS probe() and continues to initialize ITS hardware.

This assumption is not perfect, sometimes we reduce memory size either because of its size crossing MAX_ORDER-1 or BASERn max size limit. The MAPD command fails if 'Device ID' is outside of device table range.

Add a simple validation check to avoid MAPD failures since we are not handling ITS command errors. This change also helps to return an error -ENOMEM instead of success to caller.

---
[v1]->[v2]
Rebase to v4.5-rc6, edit commit text and simplify code changes

 drivers/irqchip/irq-gic-v3-its.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +10 -0
index 43dfd15..6d986ac 100644
@@ -55,6 +55,16 @@ struct its_collection {
 };
 
 /*
+ * The ITS_BASER structure - contains memeory information and table
+ * entry size in bytes.
+ */
+struct its_baser {
+	void		*base;
+	u32		order;
+	u32		entry_size;
+};
+
+/*
  * The ITS structure - contains most of the infrastructure, with the
  * top-level MSI domain, the command queue, the collections, and the
  * list of devices writing to it.
@@ -66,14 +76,12 @@ struct its_node {
 	unsigned long		phys_base;
 	struct its_cmd_block	*cmd_base;
 	struct its_cmd_block	*cmd_write;

drivers/irqchip/irq-gic-v3-its.c struct its_node { · line 84

Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +2 -4
-	struct {
-		void		*base;
-		u32		order;
-	} tables[GITS_BASER_NR_REGS];
+	struct its_baser	tables[GITS_BASER_NR_REGS];
 	struct its_collection	*collections;
 	struct list_head	its_device_list;
 	u64			flags;
 	u32			ite_size;
+	struct its_baser	*device_table;

nit: grouping this field with the tables array would make it slightly nicer.

Quoted 20 lines
drivers/irqchip/irq-gic-v3-its.c +2 -0
 };
 
 #define ITS_ITT_ALIGN		SZ_256
@@ -860,6 +868,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
 		 * For other tables, only allocate a single page.
 		 */
 		if (type == GITS_BASER_TYPE_DEVICE) {
+			its->device_table = &its->tables[i];
 			/*
 			 * 'order' was initialized earlier to the default page
 			 * granule of the the ITS.  We can't have an allocation
@@ -874,6 +883,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
 					node_name, order);
 			}
 		}
+		its->tables[i].entry_size = entry_size;
 
 retry_alloc_baser:
 		alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);

drivers/irqchip/irq-gic-v3-its.c static struct its_device *its_create_device(struct its_node *its, u32 dev_id, · line 1168

Quoted 7 lines
drivers/irqchip/irq-gic-v3-its.c +4 -0
@@ -1152,6 +1162,12 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
 	int nr_ites;
 	int sz;
 
+	/* Don't allow 'dev_id' that exceeds single, flat table limit */
+	if (its->device_table &&
+	    (dev_id >= (PAGE_ORDER_TO_SIZE(its->device_table->order) /
+	    its->device_table->entry_size)))

Assume for a minute we do not have a device table (which would be perfectly possible - just think of an ITS with its own private memory, like we have with KVM). Shouldn't we also check devid with the number of bits that this ITS implements for Device IDs?

drivers/irqchip/irq-gic-v3-its.c static struct its_device *its_create_device(struct its_node *its, u32 dev_id, · line 1173

Quoted 5 lines
drivers/irqchip/irq-gic-v3-its.c +2 -0
+		return NULL;
+
 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 	/*
 	 * At least one bit of EventID is being used, hence a minimum

Thanks,

	M.
Signature · 1 line
Jazz is not dead. It just smells funny.
#4 lore raw

Hi Marc,

Quoted 56 lines · Marc Zyngier

On Sun, 28 Feb 2016 13:22:03 -0600
Shanker Donthineni <[email protected]> wrote:

Hi Shanker,

Quoted 51 lines

We are not checking whether the requested device identifier fits into the device table memory or not. The function its_create_device() assumes that enough memory has been allocated for whole DevID sparse (reported by ITS_TYPER.Devbits) during the ITS probe() and continues to initialize ITS hardware.

This assumption is not perfect, sometimes we reduce memory size either because of its size crossing MAX_ORDER-1 or BASERn max size limit. The MAPD command fails if 'Device ID' is outside of device table range.

Add a simple validation check to avoid MAPD failures since we are not handling ITS command errors. This change also helps to return an error -ENOMEM instead of success to caller.

---
[v1]->[v2]
Rebase to v4.5-rc6, edit commit text and simplify code changes

 drivers/irqchip/irq-gic-v3-its.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +10 -4
index 43dfd15..6d986ac 100644
@@ -55,6 +55,16 @@ struct its_collection {
 };
 
 /*
+ * The ITS_BASER structure - contains memeory information and table
+ * entry size in bytes.
+ */
+struct its_baser {
+	void		*base;
+	u32		order;
+	u32		entry_size;
+};
+
+/*
  * The ITS structure - contains most of the infrastructure, with the
  * top-level MSI domain, the command queue, the collections, and the
  * list of devices writing to it.
@@ -66,14 +76,12 @@ struct its_node {
 	unsigned long		phys_base;
 	struct its_cmd_block	*cmd_base;
 	struct its_cmd_block	*cmd_write;
-	struct {
-		void		*base;
-		u32		order;
-	} tables[GITS_BASER_NR_REGS];
Quoted 8 lines

drivers/irqchip/irq-gic-v3-its.c struct its_node { · line 84

Quoted 6 lines
drivers/irqchip/irq-gic-v3-its.c +2 -0
+	struct its_baser	tables[GITS_BASER_NR_REGS];
 	struct its_collection	*collections;
 	struct list_head	its_device_list;
 	u64			flags;
 	u32			ite_size;
+	struct its_baser	*device_table;

nit: grouping this field with the tables array would make it slightly nicer.

Sure, I will get rid of this variable and add a simple accessory function to retrieve device table pointer.

static struct its_baser *its_get_baser(struct its_node *its, u8 type)

{
        int i;

        for (i = 0; i < GITS_BASER_NR_REGS; i++) {
                if (its->tables[i].type == type)
                        return &its->tables[i];
        }

        return NULL;
}
Quoted 23 lines
Quoted 23 lines
drivers/irqchip/irq-gic-v3-its.c +2 -0
 };
 
 #define ITS_ITT_ALIGN		SZ_256
@@ -860,6 +868,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
 		 * For other tables, only allocate a single page.
 		 */
 		if (type == GITS_BASER_TYPE_DEVICE) {
+			its->device_table = &its->tables[i];
 			/*
 			 * 'order' was initialized earlier to the default page
 			 * granule of the the ITS.  We can't have an allocation
@@ -874,6 +883,7 @@ static int its_alloc_tables(const char *node_name, struct its_node *its)
 					node_name, order);
 			}
 		}
+		its->tables[i].entry_size = entry_size;
 
 retry_alloc_baser:
 		alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
@@ -1152,6 +1162,12 @@ static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
 	int nr_ites;
 	int sz;
 
Quoted 8 lines

drivers/irqchip/irq-gic-v3-its.c static struct its_device *its_create_device(struct its_node *its, u32 dev_id, · line 1168

Quoted 4 lines
drivers/irqchip/irq-gic-v3-its.c +4 -0
+	/* Don't allow 'dev_id' that exceeds single, flat table limit */
+	if (its->device_table &&
+	    (dev_id >= (PAGE_ORDER_TO_SIZE(its->device_table->order) /
+	    its->device_table->entry_size)))

Assume for a minute we do not have a device table (which would be perfectly possible - just think of an ITS with its own private memory, like we have with KVM). Shouldn't we also check devid with the number of bits that this ITS implements for Device IDs?

Thanks, I thought of same thing. I am planning to handle second validation check with code changes something like shown below.

        /* Don't allow 'dev_id' that exceeds single, flat table limit */
        if (baser) {
            if (dev_id >= (PAGE_ORDER_TO_SIZE(baser->order) /
                baser->entry_size))
                return NULL;
        } else if (ilog2(dev_id) >= its->device_ids)
                return NULL;
Quoted 8 lines

drivers/irqchip/irq-gic-v3-its.c static struct its_device *its_create_device(struct its_node *its, u32 dev_id, · line 1173

Quoted 5 lines
drivers/irqchip/irq-gic-v3-its.c +2 -0
+		return NULL;
+
 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 	/*
 	 * At least one bit of EventID is being used, hence a minimum

Thanks,

	M.
Signature · 3 lines
Shanker Donthineni
Qualcomm Technologies, Inc. on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project