Skip to contentSkip to conversation

hda: add bounds checking for the codec command fields

PATCH In review Wu Fengguang 3 participants 8 messages Jul 17, 2009 – Jul 20, 2009 2 messages not in the archive Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hda: add bounds checking for the codec command fields

A recent bug involves passing auto detected >0x7f NID to codec command, creating an invalid codec addr field, and finally lead to cmd timeout and fall back into single command mode. Jaroslav fixed that bug in alc880_parse_auto_config().

It would be safer to further check the bounds of all cmd fields.

Cc Jaroslav Kysela <[email protected]>

---

sound/pci/hda/hda_codec.c +10 -0
index 462e2ce..7d09650 100644
@@ -150,6 +150,16 @@ make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
 {
 	u32 val;
 
+	if ((direct & ~1) || (nid & ~0x7f) ||
+	    (verb & ~0xfff) || (parm & ~0xff)) {
+		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
+		       codec->addr, direct, nid, verb, parm);
+		direct &= 1;
+		nid  &= 0x7f;
+		verb &= 0xfff;
+		parm &= 0xff;
+	}
+
 	val = (u32)(codec->addr & 0x0f) << 28;
 	val |= (u32)direct << 27;
 	val |= (u32)nid << 20;
#1 lore raw
Quoted 15 lines · Wu Fengguang

A recent bug involves passing auto detected >0x7f NID to codec command, creating an invalid codec addr field, and finally lead to cmd timeout and fall back into single command mode. Jaroslav fixed that bug in alc880_parse_auto_config().

It would be safer to further check the bounds of all cmd fields.

Cc Jaroslav Kysela <[email protected]>

---

sound/pci/hda/hda_codec.c +0 -0
index 462e2ce..7d09650 100644

sound/pci/hda/hda_codec.c make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct, · line 156

Quoted 7 lines
sound/pci/hda/hda_codec.c +4 -0
@@ -150,6 +150,16 @@ make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
 {
 	u32 val;
 
+	if ((direct & ~1) || (nid & ~0x7f) ||
+	    (verb & ~0xfff) || (parm & ~0xff)) {
+		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
+		       codec->addr, direct, nid, verb, parm);

Maybe we shall simply return here?

Quoted 9 lines
sound/pci/hda/hda_codec.c +6 -0
+		direct &= 1;
+		nid  &= 0x7f;
+		verb &= 0xfff;
+		parm &= 0xff;
+	}
+
 	val = (u32)(codec->addr & 0x0f) << 28;
 	val |= (u32)direct << 27;
 	val |= (u32)nid << 20;
#2 lore raw

PATCH 1/1 hda: add bounds checking for the codec command fields v2

Quoted 19 lines · Wu Fengguang
Quoted 18 lines · Wu Fengguang

A recent bug involves passing auto detected >0x7f NID to codec command, creating an invalid codec addr field, and finally lead to cmd timeout and fall back into single command mode. Jaroslav fixed that bug in alc880_parse_auto_config().

It would be safer to further check the bounds of all cmd fields.

Cc Jaroslav Kysela <[email protected]>

---

sound/pci/hda/hda_codec.c +0 -0
index 462e2ce..7d09650 100644
@@ -150,6 +150,16 @@ make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
 {
 	u32 val;
 
Quoted 6 lines

sound/pci/hda/hda_codec.c make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct, · line 156

Quoted 4 lines
sound/pci/hda/hda_codec.c +4 -0
+	if ((direct & ~1) || (nid & ~0x7f) ||
+	    (verb & ~0xfff) || (parm & ~0xff)) {
+		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
+		       codec->addr, direct, nid, verb, parm);

Maybe we shall simply return here?

Here it is :)

---
hda: add bounds checking for the codec command fields

A recent bug involves passing auto detected >0x7f NID to codec command, creating an invalid codec addr field, and finally lead to cmd timeout and fall back into single command mode. Jaroslav fixed that bug in alc880_parse_auto_config().

It would be safer to further check the bounds of all cmd fields.

Cc Jaroslav Kysela <[email protected]>
---
 sound/pci/hda/hda_codec.c |   12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
sound/pci/hda/hda_codec.c +11 -1
@@ -150,7 +150,14 @@ make_codec_cmd(struct hda_codec *codec, 
 {
 	u32 val;
 
-	val = (u32)(codec->addr & 0x0f) << 28;
+	if ((codec->addr & ~0xf) | (direct & ~1) | (nid & ~0x7f) |
+	    (verb & ~0xfff) | (parm & ~0xff)) {
+		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
+		       codec->addr, direct, nid, verb, parm);
+		return ~0;
+	}
+
+	val = (u32)codec->addr << 28;
 	val |= (u32)direct << 27;
 	val |= (u32)nid << 20;
 	val |= verb << 8;
@@ -167,6 +174,9 @@ static int codec_exec_verb(struct hda_co
 	struct hda_bus *bus = codec->bus;
 	int err;
 
+	if (cmd == ~0)
+		return -1;
+
 	if (res)
 		*res = -1;
  again:
#3 lore raw

Re: [PATCH] hda: add bounds checking for the codec command fields v2

At Fri, 17 Jul 2009 16:49:19 +0800,

Quoted 38 lines · Wu Fengguang
Quoted 19 lines · Wu Fengguang
Quoted 18 lines · Wu Fengguang

A recent bug involves passing auto detected >0x7f NID to codec command, creating an invalid codec addr field, and finally lead to cmd timeout and fall back into single command mode. Jaroslav fixed that bug in alc880_parse_auto_config().

It would be safer to further check the bounds of all cmd fields.

Cc Jaroslav Kysela <[email protected]>

---

sound/pci/hda/hda_codec.c +0 -0
index 462e2ce..7d09650 100644
@@ -150,6 +150,16 @@ make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
 {
 	u32 val;
 
Quoted 6 lines

sound/pci/hda/hda_codec.c make_codec_cmd(struct hda_codec *codec, · line 156

Quoted 4 lines
sound/pci/hda/hda_codec.c +4 -0
+	if ((direct & ~1) || (nid & ~0x7f) ||
+	    (verb & ~0xfff) || (parm & ~0xff)) {
+		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
+		       codec->addr, direct, nid, verb, parm);

Maybe we shall simply return here?

Here it is :)

---
hda: add bounds checking for the codec command fields

A recent bug involves passing auto detected >0x7f NID to codec command, creating an invalid codec addr field, and finally lead to cmd timeout and fall back into single command mode. Jaroslav fixed that bug in alc880_parse_auto_config().

Quoted 4 lines

It would be safer to further check the bounds of all cmd fields.

Cc Jaroslav Kysela <[email protected]>

Looks good. Applied now.

Thanks!

Takashi

Quoted 33 lines
---
 sound/pci/hda/hda_codec.c |   12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
sound/pci/hda/hda_codec.c +11 -1
@@ -150,7 +150,14 @@ make_codec_cmd(struct hda_codec *codec, 
 {
 	u32 val;
 
-	val = (u32)(codec->addr & 0x0f) << 28;
+	if ((codec->addr & ~0xf) | (direct & ~1) | (nid & ~0x7f) |
+	    (verb & ~0xfff) | (parm & ~0xff)) {
+		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
+		       codec->addr, direct, nid, verb, parm);
+		return ~0;
+	}
+
+	val = (u32)codec->addr << 28;
 	val |= (u32)direct << 27;
 	val |= (u32)nid << 20;
 	val |= verb << 8;
@@ -167,6 +174,9 @@ static int codec_exec_verb(struct hda_co
 	struct hda_bus *bus = codec->bus;
 	int err;
 
+	if (cmd == ~0)
+		return -1;
+
 	if (res)
 		*res = -1;
  again:

A message that is not in the archive (sent off-list or not yet received): BB1F052FCDB1EA468BD99786C8B1ED2C05871FFCB9@PDSMSX501.ccr.corp.intel.com

A message that is not in the archive (sent off-list or not yet received): s5hljmn8u52.wl%[email protected]

#6 lore raw

Re: [PATCH] hda: add bounds checking for the codec command fields v2

At Fri, 17 Jul 2009 12:46:01 +0200,

Quoted 9 lines · I wrote:

At Fri, 17 Jul 2009 18:41:05 +0800,

Quoted 3 lines · Chaohong

Although it does address this issue, I am not comfortable with this fixing. It seems more like a workaround than fix.

No, it's rather for catching a bug. This is definitely neither "workaround" nor "fix".

BTW, I fixed the patch again as below.

Takashi

---

Quoted 1 line

From 82e1b804b03defe46fb69ffd2c8b19e6649bcb0d Mon Sep 17 00:00:00 2001

From: Takashi Iwai <[email protected]>
Date: Fri, 17 Jul 2009 12:47:34 +0200
Subject: [PATCH] ALSA: hda - Fix the previous sanity check in make_codec_cmd()

The newly added sanity-check for a codec verb can be better written with logical ORs. Also, the parameter can be more than 8bit.

---
 sound/pci/hda/hda_codec.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
sound/pci/hda/hda_codec.c +2 -2
index d9d3262..35f0f22 100644
@@ -150,8 +150,8 @@ make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
 {
 	u32 val;
 
-	if ((codec->addr & ~0xf) | (direct & ~1) | (nid & ~0x7f) |
-	    (verb & ~0xfff) | (parm & ~0xff)) {
+	if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
+	    (verb & ~0xfff) || (parm & ~0xffff)) {
 		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
 		       codec->addr, direct, nid, verb, parm);
 		return ~0;
Signature · 1 line
1.6.3.2
#7 lore raw

Re: [PATCH] hda: add bounds checking for the codec command fields v2

Quoted 17 lines · Takashi Iwai

At Fri, 17 Jul 2009 12:46:01 +0200,

Quoted 9 lines · I wrote:

At Fri, 17 Jul 2009 18:41:05 +0800,

Quoted 3 lines · Chaohong

Although it does address this issue, I am not comfortable with this fixing. It seems more like a workaround than fix.

No, it's rather for catching a bug. This is definitely neither "workaround" nor "fix".

BTW, I fixed the patch again as below.

Takashi

Quoted 8 lines

---

Quoted 1 line

From 82e1b804b03defe46fb69ffd2c8b19e6649bcb0d Mon Sep 17 00:00:00 2001

From: Takashi Iwai <[email protected]>
Date: Fri, 17 Jul 2009 12:47:34 +0200
Subject: [PATCH] ALSA: hda - Fix the previous sanity check in make_codec_cmd()

The newly added sanity-check for a codec verb can be better written with logical ORs. Also, the parameter can be more than 8bit.

Ah OK, thanks for the fix!

Thanks,
Fengguang

#8 lore raw

Re: [PATCH] hda: add bounds checking for the codec command fields v2

In reply to a message not in the archive
Quoted 8 lines · Takashi Iwai

At Fri, 17 Jul 2009 18:41:05 +0800,

Quoted 3 lines · Chaohong

Although it does address this issue, I am not comfortable with this fixing. It seems more like a workaround than fix.

No, it's rather for catching a bug. This is definitely neither "workaround" nor "fix".

Yes, I wrote this mainly for catching unknown bugs.

Quoted 6 lines
Quoted 2 lines

Moreover, if long format node
ID is used in the future, the code will cause little trouble for maintaining.

Well, the current code doesn't support the long id (as the restriction of HD-audio controller side), so we'd need major changes in anyway. Thus this check is no big issue for maintenance, at least to me :)

Does the HDA spec define cmd format that accept long form NID? If so, can you point me to the specific location please? Thanks.

Quoted 4 lines
Quoted 2 lines

what I want is to fix it during parsing connection list, and verify the node id is valid there .

Heh, this was already fixed :)

AFAIK, snd_hda_get_connections() won't return NIDs bigger than 0x7f(short) or 0x7ffff(long).

Thanks,
Fengguang

#9 lore raw

Re: [PATCH] hda: add bounds checking for the codec command fields v2

Quoted 11 lines

Cc: Guo, Chaohong; [email protected]; Jaroslav Kysela; John Villalovos
Subject: Re: [PATCH] hda: add bounds checking for the codec command fields v2

Quoted 3 lines · Takashi Iwai

At Fri, 17 Jul 2009 18:41:05 +0800,

Quoted 1 line · Chaohong
Quoted 8 lines
Quoted 1 line
Quoted 1 line

Although it does address this issue, I am not comfortable

with this fixing.

Quoted 4 lines
Quoted 1 line

It seems more like a workaround than fix.

No, it's rather for catching a bug. This is definitely neither "workaround" nor "fix".

Yes, I wrote this mainly for catching unknown bugs.

Oh, I misunderstood your intention. So, we still need to investigate the bug which occurs on RH i386 version :)

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Moreover, if long format node
ID is used in the future, the code will cause little

trouble for maintaining.

Quoted 1 line
Quoted 7 lines
Quoted 1 line

Well, the current code doesn't support the long id (as the

restriction

Quoted 2 lines

of HD-audio controller side), so we'd need major changes in anyway. Thus this check is no big issue for maintenance, at least to me :)

Does the HDA spec define cmd format that accept long form NID? If so, can you point me to the specific location please? Thanks.

No. AFAIK, HDA doesn't specifiy long NID verb yet, I said "In the
feature". but who knows why hardware vendors want to use long
NID in the future, seems 128 is enough.

-minskey

Quoted 13 lines
Quoted 2 lines
Quoted 2 lines

what I want is to fix it during parsing connection list,

and verify the node

Quoted 3 lines
Quoted 1 line

id is valid there .

Heh, this was already fixed :)

AFAIK, snd_hda_get_connections() won't return NIDs bigger than 0x7f(short) or 0x7ffff(long).

Thanks,
Fengguang