Skip to contentSkip to conversation

igt_kms: Fix use after free in kmstest_get_pipe_from_crtc_id

PATCH In review Tvrtko Ursulin 2 participants 6 messages Apr 27, 2016 – Apr 28, 2016 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 igt_kms: Fix use after free in kmstest_get_pipe_from_crtc_id

From: Tvrtko Ursulin <[email protected]>

---
 lib/igt_kms.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/igt_kms.c +2 -2
index ef24a4965567..7557bdc20fa4 100644
@@ -402,10 +402,10 @@ int kmstest_get_pipe_from_crtc_id(int fd, int crtc_id)
 			break;
 	}
 
-	drmModeFreeResources(res);
-
 	igt_assert(i < res->count_crtcs);
 
+	drmModeFreeResources(res);
+
 	return i;
 }
 
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 2/3 igt_kms: Allow kmstest_get_connector_config to take provided drmModeGetResources

From: Tvrtko Ursulin <[email protected]>

This will enable the following patch to generate less dmesg spam.

---
 lib/igt_kms.c       | 33 +++++++++++++++++++++++----------
 lib/igt_kms.h       |  3 ++-
 tests/kms_3d.c      |  2 +-
 tests/kms_flip.c    |  7 ++++---
 tests/kms_render.c  |  5 +++--
 tests/testdisplay.c |  2 +-
 6 files changed, 34 insertions(+), 18 deletions(-)
lib/igt_kms.c +23 -10
index 7557bdc20fa4..07f523e2c39b 100644
@@ -749,6 +749,7 @@ bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
 /**
  * _kmstest_connector_config:
  * @drm_fd: DRM fd
+ * @resources: pointer to drmModeRes or NULL
  * @connector_id: DRM connector id
  * @crtc_idx_mask: mask of allowed DRM CRTC indices
  * @config: structure filled with the possible configuration
@@ -757,17 +758,24 @@ bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
  * This tries to find a suitable configuration for the given connector and CRTC
  * constraint and fills it into @config.
  */
-static bool _kmstest_connector_config(int drm_fd, uint32_t connector_id,
+static bool _kmstest_connector_config(int drm_fd, drmModeRes *resources,
+				      uint32_t connector_id,
 				      unsigned long crtc_idx_mask,
 				      struct kmstest_connector_config *config,
 				      bool probe)
 {
-	drmModeRes *resources;
 	drmModeConnector *connector;
 	drmModeEncoder *encoder;
+	bool free_resources;
 	int i, j;
 
-	resources = drmModeGetResources(drm_fd);
+	if (resources == NULL) {
+		resources = drmModeGetResources(drm_fd);
+		free_resources = true;
+	} else {
+		free_resources = false;
+	}
+
 	if (!resources) {
 		igt_warn("drmModeGetResources failed");
 		goto err1;
@@ -840,7 +848,8 @@ found:
 	config->pipe = kmstest_get_pipe_from_crtc_id(drm_fd,
 						     config->crtc->crtc_id);
 
-	drmModeFreeResources(resources);
+	if (free_resources)
+		drmModeFreeResources(resources);
 
 	return true;
 err4:
@@ -848,7 +857,8 @@ err4:
 err3:
 	drmModeFreeConnector(connector);
 err2:
-	drmModeFreeResources(resources);
+	if (free_resources)
+		drmModeFreeResources(resources);
 err1:
 	return false;
 }
@@ -856,6 +866,7 @@ err1:
 /**
  * kmstest_get_connector_config:
  * @drm_fd: DRM fd
+ * @resources: pointer to drmModeRes or NULL
  * @connector_id: DRM connector id
  * @crtc_idx_mask: mask of allowed DRM CRTC indices
  * @config: structure filled with the possible configuration
@@ -863,12 +874,13 @@ err1:
  * This tries to find a suitable configuration for the given connector and CRTC
  * constraint and fills it into @config.
  */
-bool kmstest_get_connector_config(int drm_fd, uint32_t connector_id,
+bool kmstest_get_connector_config(int drm_fd, drmModeRes *resources,
+				  uint32_t connector_id,
 				  unsigned long crtc_idx_mask,
 				  struct kmstest_connector_config *config)
 {
-	return _kmstest_connector_config(drm_fd, connector_id, crtc_idx_mask,
-					 config, 0);
+	return _kmstest_connector_config(drm_fd, resources, connector_id,
+					 crtc_idx_mask, config, 0);
 }
 
 /**
@@ -886,8 +898,8 @@ bool kmstest_probe_connector_config(int drm_fd, uint32_t connector_id,
 				    unsigned long crtc_idx_mask,
 				    struct kmstest_connector_config *config)
 {
-	return _kmstest_connector_config(drm_fd, connector_id, crtc_idx_mask,
-					 config, 1);
+	return _kmstest_connector_config(drm_fd, NULL, connector_id,
+					 crtc_idx_mask, config, 1);
 }
 
 /**
@@ -1160,6 +1172,7 @@ static void igt_output_refresh(igt_output_t *output)
 		kmstest_free_connector_config(&output->config);
 
 	ret = kmstest_get_connector_config(display->drm_fd,
+					   NULL,
 					   output->id,
 					   crtc_idx_mask,
 					   &output->config);
lib/igt_kms.h +2 -1
index 5c8340171ab6..8cbba1673d28 100644
@@ -158,7 +158,8 @@ void kmstest_force_edid(int drm_fd, drmModeConnector *connector,
 
 bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
 					drmModeModeInfo *mode);
-bool kmstest_get_connector_config(int drm_fd, uint32_t connector_id,
+bool kmstest_get_connector_config(int drm_fd, drmModeRes *resources,
+				  uint32_t connector_id,
 				  unsigned long crtc_idx_mask,
 				  struct kmstest_connector_config *config);
 bool kmstest_probe_connector_config(int drm_fd, uint32_t connector_id,
tests/kms_3d.c +1 -1
index bfc981ee279d..e8bc4a42d8aa 100644
@@ -86,7 +86,7 @@ igt_simple_main
 			continue;
 
 		/* create a configuration */
-		ret = kmstest_get_connector_config(drm_fd, connector_id,
+		ret = kmstest_get_connector_config(drm_fd, NULL, connector_id,
 						   crtc_mask, &config);
 		if (ret != true) {
 			igt_info("Error creating configuration for:\n  ");
tests/kms_flip.c +4 -3
index eda2fcc9212d..fec69254e4da 100644
@@ -1042,7 +1042,8 @@ static void connector_find_preferred_mode(uint32_t connector_id, int crtc_idx,
 {
 	struct kmstest_connector_config config;
 
-	if (!kmstest_get_connector_config(drm_fd, connector_id, 1 << crtc_idx,
+	if (!kmstest_get_connector_config(drm_fd, NULL,
+					  connector_id, 1 << crtc_idx,
 					  &config)) {
 		o->mode_valid = 0;
 		return;
@@ -1086,11 +1087,11 @@ static void connector_find_compatible_mode(int crtc_idx0, int crtc_idx1,
 	drmModeModeInfo *mode[2];
 	int n, m;
 
-	if (!kmstest_get_connector_config(drm_fd, o->_connector[0],
+	if (!kmstest_get_connector_config(drm_fd, NULL, o->_connector[0],
 					  1 << crtc_idx0, &config[0]))
 		return;
 
-	if (!kmstest_get_connector_config(drm_fd, o->_connector[1],
+	if (!kmstest_get_connector_config(drm_fd, NULL, o->_connector[1],
 					  1 << crtc_idx1, &config[1])) {
 		kmstest_free_connector_config(&config[0]);
 		return;
tests/kms_render.c +3 -2
index 72da87f19af3..735a1e117349 100644
@@ -210,8 +210,9 @@ static int run_test(const char *test_name, enum test_flags flags)
 		for (j = 0; j < resources->count_crtcs; j++) {
 			struct kmstest_connector_config cconf;
 
-			if (!kmstest_get_connector_config(drm_fd, connector_id,
-							   1 << j, &cconf))
+			if (!kmstest_get_connector_config(drm_fd, NULL,
+							  connector_id,
+							  1 << j, &cconf))
 				continue;
 
 			test_connector(test_name, &cconf, flags);
tests/testdisplay.c +1 -1
index 00b47bd06280..9d84d8cd4000 100644
@@ -201,7 +201,7 @@ static void connector_find_preferred_mode(uint32_t connector_id,
 		ret = kmstest_probe_connector_config(drm_fd, connector_id,
 						     crtc_idx_mask, &config);
 	else
-		ret = kmstest_get_connector_config(drm_fd, connector_id,
+		ret = kmstest_get_connector_config(drm_fd, NULL, connector_id,
 						   crtc_idx_mask, &config);
 	if (!ret) {
 		c->mode_valid = 0;
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

Re: [PATCH i-g-t 2/3] igt_kms: Allow kmstest_get_connector_config to take provided drmModeGetResources

Quoted 40 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

This will enable the following patch to generate less dmesg spam.

---
 lib/igt_kms.c       | 33 +++++++++++++++++++++++----------
 lib/igt_kms.h       |  3 ++-
 tests/kms_3d.c      |  2 +-
 tests/kms_flip.c    |  7 ++++---
 tests/kms_render.c  |  5 +++--
 tests/testdisplay.c |  2 +-
 6 files changed, 34 insertions(+), 18 deletions(-)
lib/igt_kms.c +3 -2
index 7557bdc20fa4..07f523e2c39b 100644
@@ -749,6 +749,7 @@ bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
 /**
  * _kmstest_connector_config:
  * @drm_fd: DRM fd
+ * @resources: pointer to drmModeRes or NULL
  * @connector_id: DRM connector id
  * @crtc_idx_mask: mask of allowed DRM CRTC indices
  * @config: structure filled with the possible configuration
@@ -757,17 +758,24 @@ bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
  * This tries to find a suitable configuration for the given connector and CRTC
  * constraint and fills it into @config.
  */
-static bool _kmstest_connector_config(int drm_fd, uint32_t connector_id,
+static bool _kmstest_connector_config(int drm_fd, drmModeRes *resources,
+				      uint32_t connector_id,
 				      unsigned long crtc_idx_mask,
 				      struct kmstest_connector_config *config,
 				      bool probe)
 {
-	drmModeRes *resources;
 	drmModeConnector *connector;
 	drmModeEncoder *encoder;

lib/igt_kms.c bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector, · line 777

Quoted 10 lines
lib/igt_kms.c +7 -1
+	bool free_resources;
 	int i, j;
 
-	resources = drmModeGetResources(drm_fd);
+	if (resources == NULL) {
+		resources = drmModeGetResources(drm_fd);
+		free_resources = true;
+	} else {
+		free_resources = false;
+	}

Imo you should just cache this internally in the lib instead of forcing everyone to become more clever. We can just request resources once when initializing the kmstest library, and then maybe provide a function to force reprobing (for something like testdisplay, but that doesn't use kmstest yet).
-Daniel

Quoted 159 lines
lib/igt_kms.c +13 -7
+
 	if (!resources) {
 		igt_warn("drmModeGetResources failed");
 		goto err1;
@@ -840,7 +848,8 @@ found:
 	config->pipe = kmstest_get_pipe_from_crtc_id(drm_fd,
 						     config->crtc->crtc_id);
 
-	drmModeFreeResources(resources);
+	if (free_resources)
+		drmModeFreeResources(resources);
 
 	return true;
 err4:
@@ -848,7 +857,8 @@ err4:
 err3:
 	drmModeFreeConnector(connector);
 err2:
-	drmModeFreeResources(resources);
+	if (free_resources)
+		drmModeFreeResources(resources);
 err1:
 	return false;
 }
@@ -856,6 +866,7 @@ err1:
 /**
  * kmstest_get_connector_config:
  * @drm_fd: DRM fd
+ * @resources: pointer to drmModeRes or NULL
  * @connector_id: DRM connector id
  * @crtc_idx_mask: mask of allowed DRM CRTC indices
  * @config: structure filled with the possible configuration
@@ -863,12 +874,13 @@ err1:
  * This tries to find a suitable configuration for the given connector and CRTC
  * constraint and fills it into @config.
  */
-bool kmstest_get_connector_config(int drm_fd, uint32_t connector_id,
+bool kmstest_get_connector_config(int drm_fd, drmModeRes *resources,
+				  uint32_t connector_id,
 				  unsigned long crtc_idx_mask,
 				  struct kmstest_connector_config *config)
 {
-	return _kmstest_connector_config(drm_fd, connector_id, crtc_idx_mask,
-					 config, 0);
+	return _kmstest_connector_config(drm_fd, resources, connector_id,
+					 crtc_idx_mask, config, 0);
 }
 
 /**
@@ -886,8 +898,8 @@ bool kmstest_probe_connector_config(int drm_fd, uint32_t connector_id,
 				    unsigned long crtc_idx_mask,
 				    struct kmstest_connector_config *config)
 {
-	return _kmstest_connector_config(drm_fd, connector_id, crtc_idx_mask,
-					 config, 1);
+	return _kmstest_connector_config(drm_fd, NULL, connector_id,
+					 crtc_idx_mask, config, 1);
 }
 
 /**
@@ -1160,6 +1172,7 @@ static void igt_output_refresh(igt_output_t *output)
 		kmstest_free_connector_config(&output->config);
 
 	ret = kmstest_get_connector_config(display->drm_fd,
+					   NULL,
 					   output->id,
 					   crtc_idx_mask,
 					   &output->config);
lib/igt_kms.h +2 -1
index 5c8340171ab6..8cbba1673d28 100644
@@ -158,7 +158,8 @@ void kmstest_force_edid(int drm_fd, drmModeConnector *connector,
 
 bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
 					drmModeModeInfo *mode);
-bool kmstest_get_connector_config(int drm_fd, uint32_t connector_id,
+bool kmstest_get_connector_config(int drm_fd, drmModeRes *resources,
+				  uint32_t connector_id,
 				  unsigned long crtc_idx_mask,
 				  struct kmstest_connector_config *config);
 bool kmstest_probe_connector_config(int drm_fd, uint32_t connector_id,
tests/kms_3d.c +1 -1
index bfc981ee279d..e8bc4a42d8aa 100644
@@ -86,7 +86,7 @@ igt_simple_main
 			continue;
 
 		/* create a configuration */
-		ret = kmstest_get_connector_config(drm_fd, connector_id,
+		ret = kmstest_get_connector_config(drm_fd, NULL, connector_id,
 						   crtc_mask, &config);
 		if (ret != true) {
 			igt_info("Error creating configuration for:\n  ");
tests/kms_flip.c +4 -3
index eda2fcc9212d..fec69254e4da 100644
@@ -1042,7 +1042,8 @@ static void connector_find_preferred_mode(uint32_t connector_id, int crtc_idx,
 {
 	struct kmstest_connector_config config;
 
-	if (!kmstest_get_connector_config(drm_fd, connector_id, 1 << crtc_idx,
+	if (!kmstest_get_connector_config(drm_fd, NULL,
+					  connector_id, 1 << crtc_idx,
 					  &config)) {
 		o->mode_valid = 0;
 		return;
@@ -1086,11 +1087,11 @@ static void connector_find_compatible_mode(int crtc_idx0, int crtc_idx1,
 	drmModeModeInfo *mode[2];
 	int n, m;
 
-	if (!kmstest_get_connector_config(drm_fd, o->_connector[0],
+	if (!kmstest_get_connector_config(drm_fd, NULL, o->_connector[0],
 					  1 << crtc_idx0, &config[0]))
 		return;
 
-	if (!kmstest_get_connector_config(drm_fd, o->_connector[1],
+	if (!kmstest_get_connector_config(drm_fd, NULL, o->_connector[1],
 					  1 << crtc_idx1, &config[1])) {
 		kmstest_free_connector_config(&config[0]);
 		return;
tests/kms_render.c +3 -2
index 72da87f19af3..735a1e117349 100644
@@ -210,8 +210,9 @@ static int run_test(const char *test_name, enum test_flags flags)
 		for (j = 0; j < resources->count_crtcs; j++) {
 			struct kmstest_connector_config cconf;
 
-			if (!kmstest_get_connector_config(drm_fd, connector_id,
-							   1 << j, &cconf))
+			if (!kmstest_get_connector_config(drm_fd, NULL,
+							  connector_id,
+							  1 << j, &cconf))
 				continue;
 
 			test_connector(test_name, &cconf, flags);
tests/testdisplay.c +1 -1
index 00b47bd06280..9d84d8cd4000 100644
@@ -201,7 +201,7 @@ static void connector_find_preferred_mode(uint32_t connector_id,
 		ret = kmstest_probe_connector_config(drm_fd, connector_id,
 						     crtc_idx_mask, &config);
 	else
-		ret = kmstest_get_connector_config(drm_fd, connector_id,
+		ret = kmstest_get_connector_config(drm_fd, NULL, connector_id,
 						   crtc_idx_mask, &config);
 	if (!ret) {
 		c->mode_valid = 0;
Signature · 6 lines
1.9.1

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

Re: [PATCH i-g-t 2/3] igt_kms: Allow kmstest_get_connector_config to take provided drmModeGetResources

Quoted 44 lines · Daniel Vetter
Quoted 43 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

This will enable the following patch to generate less dmesg spam.

---
  lib/igt_kms.c       | 33 +++++++++++++++++++++++----------
  lib/igt_kms.h       |  3 ++-
  tests/kms_3d.c      |  2 +-
  tests/kms_flip.c    |  7 ++++---
  tests/kms_render.c  |  5 +++--
  tests/testdisplay.c |  2 +-
  6 files changed, 34 insertions(+), 18 deletions(-)
lib/igt_kms.c +4 -2
index 7557bdc20fa4..07f523e2c39b 100644
@@ -749,6 +749,7 @@ bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
  /**
   * _kmstest_connector_config:
   * @drm_fd: DRM fd
+ * @resources: pointer to drmModeRes or NULL
   * @connector_id: DRM connector id
   * @crtc_idx_mask: mask of allowed DRM CRTC indices
   * @config: structure filled with the possible configuration
@@ -757,17 +758,24 @@ bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector,
   * This tries to find a suitable configuration for the given connector and CRTC
   * constraint and fills it into @config.
   */
-static bool _kmstest_connector_config(int drm_fd, uint32_t connector_id,
+static bool _kmstest_connector_config(int drm_fd, drmModeRes *resources,
+				      uint32_t connector_id,
  				      unsigned long crtc_idx_mask,
  				      struct kmstest_connector_config *config,
  				      bool probe)
  {
-	drmModeRes *resources;
  	drmModeConnector *connector;
  	drmModeEncoder *encoder;
+	bool free_resources;
  	int i, j;
 
Quoted 13 lines

lib/igt_kms.c bool kmstest_get_connector_default_mode(int drm_fd, drmModeConnector *connector, · line 777

Quoted 7 lines
lib/igt_kms.c +6 -1
-	resources = drmModeGetResources(drm_fd);
+	if (resources == NULL) {
+		resources = drmModeGetResources(drm_fd);
+		free_resources = true;
+	} else {
+		free_resources = false;
+	}

Imo you should just cache this internally in the lib instead of forcing everyone to become more clever. We can just request resources once when initializing the kmstest library, and then maybe provide a function to force reprobing (for something like testdisplay, but that doesn't use kmstest yet).

If you merge the two drm core patches patches 2/3 and 3/3 are best abandoned. :)

Regards,

Tvrtko

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

#4 lore raw

PATCH 3/3 kms_flip: Reduce dmesg spam by not calling drmModeGetResources too much

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Configuration reported in drmModeGetResources is static - pass it into kmstest_get_connector_config so it can be reused there.

---
 tests/kms_flip.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/kms_flip.c +3 -3
index fec69254e4da..a85153fa3d52 100644
@@ -1042,7 +1042,7 @@ static void connector_find_preferred_mode(uint32_t connector_id, int crtc_idx,
 {
 	struct kmstest_connector_config config;
 
-	if (!kmstest_get_connector_config(drm_fd, NULL,
+	if (!kmstest_get_connector_config(drm_fd, resources,
 					  connector_id, 1 << crtc_idx,
 					  &config)) {
 		o->mode_valid = 0;
@@ -1087,11 +1087,11 @@ static void connector_find_compatible_mode(int crtc_idx0, int crtc_idx1,
 	drmModeModeInfo *mode[2];
 	int n, m;
 
-	if (!kmstest_get_connector_config(drm_fd, NULL, o->_connector[0],
+	if (!kmstest_get_connector_config(drm_fd, resources, o->_connector[0],
 					  1 << crtc_idx0, &config[0]))
 		return;
 
-	if (!kmstest_get_connector_config(drm_fd, NULL, o->_connector[1],
+	if (!kmstest_get_connector_config(drm_fd, resources, o->_connector[1],
 					  1 << crtc_idx1, &config[1])) {
 		kmstest_free_connector_config(&config[0]);
 		return;
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw
In reply to Tvrtko Ursulin
Quoted 15 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

---
  lib/igt_kms.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
lib/igt_kms.c +0 -0
index ef24a4965567..7557bdc20fa4 100644
@@ -402,10 +402,10 @@ int kmstest_get_pipe_from_crtc_id(int fd, int crtc_id)
  			break;
  	}

lib/igt_kms.c int kmstest_get_pipe_from_crtc_id(int fd, int crtc_id) · line 411

Quoted 10 lines
lib/igt_kms.c +2 -2
 
-	drmModeFreeResources(res);
-
  	igt_assert(i < res->count_crtcs);
 
+	drmModeFreeResources(res);
+
  	return i;
  }
 

I've pushed this since it is obvious.

Regards,

Tvrtko

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