Skip to contentSkip to conversation

Error in inner loop in validate_cmds_sorted / out of bounds issue

DISCUSSION Hanno Böck 3 participants 8 messages Jul 26, 2015 – Jul 29, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

I was trying to track down an out of bounds read issue in the intel drm driver that got reported by kasan.

It happens in the function validate_cmds_sorted (i915_cmd_parser.c), where there are two nested loops, this is the relevant code part:

	for (i = 0; i < cmd_table_count; i++) {
		const struct drm_i915_cmd_table *table = &cmd_tables[i];
		u32 previous = 0;
		int j;

		for (j = 0; j < table->count; j++) {
			const struct drm_i915_cmd_descriptor *desc =
				&table->table[i];

Now that &table->table[i] should probably really be &table->table[j], because that's the counter variable of the inner loop. Otherwise it doesn't make any sense (the inner loop would just repeat doing the same thing multiple times).
However if I try to change [i] to [j] here my system doesn't boot any more, I just get a black screen. So I assume this bug is somehow hiding another more severe bug.

cu,

Signature · 5 lines
Hanno Böck
http://hboeck.de/

mail/jabber: [email protected]
GPG: BBB51E42

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

#1 lore raw
Quoted 17 lines · Hanno Böck

Hi,

I was trying to track down an out of bounds read issue in the intel drm driver that got reported by kasan.

It happens in the function validate_cmds_sorted (i915_cmd_parser.c), where there are two nested loops, this is the relevant code part:

	for (i = 0; i < cmd_table_count; i++) {
		const struct drm_i915_cmd_table *table = &cmd_tables[i];
		u32 previous = 0;
		int j;

		for (j = 0; j < table->count; j++) {
			const struct drm_i915_cmd_descriptor *desc =
				&table->table[i];
Quoted 7 lines

Now that &table->table[i] should probably really be &table->table[j], because that's the counter variable of the inner loop. Otherwise it doesn't make any sense (the inner loop would just repeat doing the same thing multiple times).
However if I try to change [i] to [j] here my system doesn't boot any more, I just get a black screen. So I assume this bug is somehow hiding another more severe bug.

The tables aren't sorted, that is worth fixing.

This should get you booting with minimal fuss if you care to track down the error.

drivers/gpu/drm/i915/i915_cmd_parser.c +15 -3
index 430571b..688e814 100644
@@ -672,6 +672,13 @@ static void fini_hash_table(struct intel_engine_cs *ring)
        }
 }
 
+#define DRM_ERROR_ON(cond, fmt, ...) ({                                        \
+       bool __cond = !!(cond);                                         \
+       if (unlikely(__cond))                                           \
+               drm_err("assertion failed, %s: " fmt, #cond, ##__VA_ARGS__); \
+       unlikely(__cond);                                               \
+})
+
 /**
  * i915_cmd_parser_init_ring() - set cmd parser related fields for a ringbuffer
  * @ring: the ringbuffer to initialize
@@ -751,11 +758,16 @@ int i915_cmd_parser_init_ring(struct intel_engine_cs *ring)
        default:
                DRM_ERROR("CMD: cmd_parser_init with unknown ring: %d\n",
                          ring->id);
-               BUG();
+               return -ENODEV;
        }
 
-       BUG_ON(!validate_cmds_sorted(ring, cmd_tables, cmd_table_count));
-       BUG_ON(!validate_regs_sorted(ring));
+       if (DRM_ERROR_ON(!validate_cmds_sorted(ring, cmd_tables, cmd_table_count),
+                        "command parser table is not sorted - required for bisetion searching\n"))
+               return -ENODEV;
+
+       if (DRM_ERROR_ON(!validate_regs_sorted(ring),
+                        "register lists are not sorted - required for bisection searching\n"))
+               return -ENODEV;
 
        WARN_ON(!hash_empty(ring->cmd_hash));
 
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw
Quoted 1 line · On Mon, 27 Jul 2015 09:59:45 +0100 Chris Wilson <[email protected]>…

The tables aren't sorted, that is worth fixing.

Attached patch should do that and fix the loop. Now it boots without errors.

Does that look okay? If so please apply.

Signature · 5 lines
Hanno Böck
http://hboeck.de/

mail/jabber: [email protected]
GPG: BBB51E42
drivers/gpu/drm/i915/i915_cmd_parser.c +2 -2
index 306d9e4..95aeb70 100644
@@ -151,8 +151,8 @@ static const struct drm_i915_cmd_descriptor render_cmds[] = {
 	CMD(  MI_ARB_ON_OFF,                    SMI,    F,  1,      R  ),
 	CMD(  MI_PREDICATE,                     SMI,    F,  1,      S  ),
 	CMD(  MI_TOPOLOGY_FILTER,               SMI,    F,  1,      S  ),
-	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_SET_APPID,                     SMI,    F,  1,      S  ),
+	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_SET_CONTEXT,                   SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_URB_CLEAR,                     SMI,   !F,  0xFF,   S  ),
 	CMD(  MI_STORE_DWORD_IMM,               SMI,   !F,  0x3F,   B,
@@ -564,7 +564,7 @@ static bool validate_cmds_sorted(struct intel_engine_cs *ring,
 
 		for (j = 0; j < table->count; j++) {
 			const struct drm_i915_cmd_descriptor *desc =
-				&table->table[i];
+				&table->table[j];
 			u32 curr = desc->cmd.value & desc->cmd.mask;
 
 			if (curr < previous) {

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

#3 lore raw
Quoted 9 lines · Hanno Böck
Quoted 1 line · On Mon, 27 Jul 2015 09:59:45 +0100 Chris Wilson <[email protected]>…

The tables aren't sorted, that is worth fixing.

Attached patch should do that and fix the loop. Now it boots without errors.

Does that look okay? If so please apply.

Indeed, nice catch. Could you please read Documentation/SubmittingPatches and apply your Signed-off-by and then we can accept this patch under your authorship.

Preferrably this is two patches, (a) fix the tables, (b) fix the validator. That way we can delay enabling the validator if we need to fix the tables for others.
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#4 lore raw
Quoted 11 lines · Chris Wilson
Quoted 9 lines · Hanno Böck
Quoted 1 line · On Mon, 27 Jul 2015 09:59:45 +0100 Chris Wilson <[email protected]>…

The tables aren't sorted, that is worth fixing.

Attached patch should do that and fix the loop. Now it boots without errors.

Does that look okay? If so please apply.

Quoted 7 lines

Indeed, nice catch. Could you please read Documentation/SubmittingPatches and apply your Signed-off-by and then we can accept this patch under your authorship.

Preferrably this is two patches, (a) fix the tables, (b) fix the validator. That way we can delay enabling the validator if we need to fix the tables for others.

Also can you please add signed-off-by lines to your patch when resubmitting? See Documentation/SubmittingPatches for all the details.

Thanks, Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw

Hi,

Quoted 7 lines · On Tue, 28 Jul 2015 10:14:51 +0200 Daniel Vetter <[email protected]> wrote:
Quoted 7 lines

Indeed, nice catch. Could you please read Documentation/SubmittingPatches and apply your Signed-off-by and then we can accept this patch under your authorship.

Preferrably this is two patches, (a) fix the tables, (b) fix the validator. That way we can delay enabling the validator if we need to fix the tables for others.

I think I have checked all tables, not just the ones used on my gpu, they should be fine. But I've splittet the patch.

Quoted 2 lines

Also can you please add signed-off-by lines to your patch when resubmitting? See Documentation/SubmittingPatches for all the details.

The patch already had a "Signed-off-by" line.

The checkpatch script complains that it doesn't like the formatting of the CMD command. However I won't change that in this patch, as this is how the whole file is formatted. If this is wanted I can submit a patch changing the formatting afterwards, but I think this is an unrelated change.

Please apply.

Signature · 5 lines
Hanno Böck
http://hboeck.de/

mail/jabber: [email protected]
GPG: BBB51E42

Properly sort cmd tables.

drivers/gpu/drm/i915/i915_cmd_parser.c +1 -1
index 306d9e4..5fdd8c8 100644
@@ -151,8 +151,8 @@ static const struct drm_i915_cmd_descriptor render_cmds[] = {
 	CMD(  MI_ARB_ON_OFF,                    SMI,    F,  1,      R  ),
 	CMD(  MI_PREDICATE,                     SMI,    F,  1,      S  ),
 	CMD(  MI_TOPOLOGY_FILTER,               SMI,    F,  1,      S  ),
-	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_SET_APPID,                     SMI,    F,  1,      S  ),
+	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_SET_CONTEXT,                   SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_URB_CLEAR,                     SMI,   !F,  0xFF,   S  ),
 	CMD(  MI_STORE_DWORD_IMM,               SMI,   !F,  0x3F,   B,

Fix loop checking cmd tables.

drivers/gpu/drm/i915/i915_cmd_parser.c +1 -1
index 306d9e4..3a53bf3 100644
@@ -564,7 +564,7 @@ static bool validate_cmds_sorted(struct intel_engine_cs *ring,
 
 		for (j = 0; j < table->count; j++) {
 			const struct drm_i915_cmd_descriptor *desc =
-				&table->table[i];
+				&table->table[j];
 			u32 curr = desc->cmd.value & desc->cmd.mask;
 
 			if (curr < previous) {

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

Reviewed-by Reviewed-by
#6 lore raw
Quoted 21 lines · Hanno Böck

Hi,

Quoted 7 lines · On Tue, 28 Jul 2015 10:14:51 +0200 Daniel Vetter <[email protected]> wrote:
Quoted 7 lines

Indeed, nice catch. Could you please read Documentation/SubmittingPatches and apply your Signed-off-by and then we can accept this patch under your authorship.

Preferrably this is two patches, (a) fix the tables, (b) fix the validator. That way we can delay enabling the validator if we need to fix the tables for others.

I think I have checked all tables, not just the ones used on my gpu, they should be fine. But I've splittet the patch.

Quoted 2 lines

Also can you please add signed-off-by lines to your patch when resubmitting? See Documentation/SubmittingPatches for all the details.

The patch already had a "Signed-off-by" line.

Quoted 16 lines

The checkpatch script complains that it doesn't like the formatting of the CMD command. However I won't change that in this patch, as this is how the whole file is formatted. If this is wanted I can submit a patch changing the formatting afterwards, but I think this is an unrelated change.

Please apply.

Signature · 7 lines
Hanno Böck
http://hboeck.de/

mail/jabber: [email protected]
GPG: BBB51E42

Properly sort cmd tables.

drm/i915: Properly sort MI coomand table

In the future, we may want to speed up command/register searching using a bisection and so we require them to be in ascending order respectively by command value or register address. However, this was not true for one pair in the MI table; make it so.

Quoted 1 line
Quoted 15 lines
drivers/gpu/drm/i915/i915_cmd_parser.c +1 -1
index 306d9e4..5fdd8c8 100644
@@ -151,8 +151,8 @@ static const struct drm_i915_cmd_descriptor render_cmds[] = {
 	CMD(  MI_ARB_ON_OFF,                    SMI,    F,  1,      R  ),
 	CMD(  MI_PREDICATE,                     SMI,    F,  1,      S  ),
 	CMD(  MI_TOPOLOGY_FILTER,               SMI,    F,  1,      S  ),
-	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_SET_APPID,                     SMI,    F,  1,      S  ),
+	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_SET_CONTEXT,                   SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_URB_CLEAR,                     SMI,   !F,  0xFF,   S  ),
 	CMD(  MI_STORE_DWORD_IMM,               SMI,   !F,  0x3F,   B,
Quoted 1 line

Fix loop checking cmd tables.

drm/i915: Fix command parser table validator

As we may like to use a bisection search on the tables in future, we need them to be ordered. For convenience we expect the compiled tables to be order and check on initialisation. However, the validator used the wrong iterators failed to spot the misordered MI tables and instead walked off into the unknown (as spotted by kasan).

Quoted 1 line
Quoted 13 lines
drivers/gpu/drm/i915/i915_cmd_parser.c +1 -1
index 306d9e4..3a53bf3 100644
@@ -564,7 +564,7 @@ static bool validate_cmds_sorted(struct intel_engine_cs *ring,
 
 		for (j = 0; j < table->count; j++) {
 			const struct drm_i915_cmd_descriptor *desc =
-				&table->table[i];
+				&table->table[j];
 			u32 curr = desc->cmd.value & desc->cmd.mask;
 
 			if (curr < previous) {
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#7 lore raw
Quoted 68 lines · Chris Wilson
Quoted 21 lines · Hanno Böck

Hi,

Quoted 7 lines · On Tue, 28 Jul 2015 10:14:51 +0200 Daniel Vetter <[email protected]> wrote:
Quoted 7 lines

Indeed, nice catch. Could you please read Documentation/SubmittingPatches and apply your Signed-off-by and then we can accept this patch under your authorship.

Preferrably this is two patches, (a) fix the tables, (b) fix the validator. That way we can delay enabling the validator if we need to fix the tables for others.

I think I have checked all tables, not just the ones used on my gpu, they should be fine. But I've splittet the patch.

Quoted 2 lines

Also can you please add signed-off-by lines to your patch when resubmitting? See Documentation/SubmittingPatches for all the details.

The patch already had a "Signed-off-by" line.

Quoted 16 lines

The checkpatch script complains that it doesn't like the formatting of the CMD command. However I won't change that in this patch, as this is how the whole file is formatted. If this is wanted I can submit a patch changing the formatting afterwards, but I think this is an unrelated change.

Please apply.

Signature · 7 lines
Hanno Böck
http://hboeck.de/

mail/jabber: [email protected]
GPG: BBB51E42

Properly sort cmd tables.

drm/i915: Properly sort MI coomand table

In the future, we may want to speed up command/register searching using a bisection and so we require them to be in ascending order respectively by command value or register address. However, this was not true for one pair in the MI table; make it so.

Quoted 1 line
Quoted 15 lines
drivers/gpu/drm/i915/i915_cmd_parser.c +1 -1
index 306d9e4..5fdd8c8 100644
@@ -151,8 +151,8 @@ static const struct drm_i915_cmd_descriptor render_cmds[] = {
 	CMD(  MI_ARB_ON_OFF,                    SMI,    F,  1,      R  ),
 	CMD(  MI_PREDICATE,                     SMI,    F,  1,      S  ),
 	CMD(  MI_TOPOLOGY_FILTER,               SMI,    F,  1,      S  ),
-	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_SET_APPID,                     SMI,    F,  1,      S  ),
+	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_SET_CONTEXT,                   SMI,   !F,  0xFF,   R  ),
 	CMD(  MI_URB_CLEAR,                     SMI,   !F,  0xFF,   S  ),
 	CMD(  MI_STORE_DWORD_IMM,               SMI,   !F,  0x3F,   B,
Quoted 1 line

Fix loop checking cmd tables.

drm/i915: Fix command parser table validator

Quoted 8 lines

As we may like to use a bisection search on the tables in future, we need them to be ordered. For convenience we expect the compiled tables to be order and check on initialisation. However, the validator used the wrong iterators failed to spot the misordered MI tables and instead walked off into the unknown (as spotted by kasan).

Quoted 1 line

Ok I hand-assembled the patches since it's your first, but please followo Documentation/Submitting patches next time around, i.e. 1 mail per patch, no attachments, diff included at the bottom. Otherwise the tools can't just pick up patches on my side. git send-email will do all that formatting for you correctly, so that's what I recommend to use.

Anyway pachtes applied to drm-intel-next-queued, thanks a lot for doing them.
-Daniel

Quoted 17 lines
Quoted 14 lines
drivers/gpu/drm/i915/i915_cmd_parser.c +1 -1
index 306d9e4..3a53bf3 100644
@@ -564,7 +564,7 @@ static bool validate_cmds_sorted(struct intel_engine_cs *ring,
 
 		for (j = 0; j < table->count; j++) {
 			const struct drm_i915_cmd_descriptor *desc =
-				&table->table[i];
+				&table->table[j];
 			u32 curr = desc->cmd.value & desc->cmd.mask;
 
 			if (curr < previous) {
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx