Skip to contentSkip to conversation

ftrace: unsigned idx cannot be less than 0

PATCH Superseded Roel Kluin 3 participants 7 messages Jan 2, 2009 – Jan 6, 2009

Subsystems (2)
lore
Share & tools
lore

Roel Kluin

author
#0 lore raw

PATCH 1/1 ftrace: unsigned idx cannot be less than 0

// vi kernel/trace/ftrace.c +787
struct ftrace_iterator {
...

        unsigned                idx;

...
};

idx is unsigned and cannot be less than 0.

---

kernel/trace/ftrace.c +1 -1
index 2f32969..a344add 100644
@@ -842,7 +842,7 @@ static void *t_start(struct seq_file *m, loff_t *pos)
 	void *p = NULL;
 
 	if (*pos > 0) {
-		if (iter->idx < 0)
+		if (iter->idx == 0)
 			return p;
 		(*pos)--;
 		iter->idx--;
#1 lore raw
Quoted 16 lines · Roel Kluin

// vi kernel/trace/ftrace.c +787
struct ftrace_iterator {
...

        unsigned                idx;

...
};

idx is unsigned and cannot be less than 0.

---

kernel/trace/ftrace.c +0 -0
index 2f32969..a344add 100644

kernel/trace/ftrace.c static void *t_start(struct seq_file *m, loff_t *pos) · line 848

Quoted 8 lines
kernel/trace/ftrace.c +1 -1
@@ -842,7 +842,7 @@ static void *t_start(struct seq_file *m, loff_t *pos)
 	void *p = NULL;
 
 	if (*pos > 0) {
-		if (iter->idx < 0)
+		if (iter->idx == 0)
 			return p;
 		(*pos)--;
 		iter->idx--;

Hi Roel,

I'm not sure this is the right fix.
If you look at t_next, if there is no more page to look at, iter_idx takes -1.

A 0 value would mean: we are in the first index on the page, which means there is something to read and we don't want to return NULL.

I guess that would be better to turn idx into a signed int.

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author
#2 lore raw
Quoted 29 lines · Frederic Weisbecker
Quoted 16 lines · Roel Kluin

// vi kernel/trace/ftrace.c +787
struct ftrace_iterator {
...

        unsigned                idx;

...
};

idx is unsigned and cannot be less than 0.

---

kernel/trace/ftrace.c +0 -0
index 2f32969..a344add 100644

kernel/trace/ftrace.c static void *t_start(struct seq_file *m, loff_t *pos) · line 848

Quoted 8 lines
kernel/trace/ftrace.c +1 -1
@@ -842,7 +842,7 @@ static void *t_start(struct seq_file *m, loff_t *pos)
 	void *p = NULL;
 
 	if (*pos > 0) {
-		if (iter->idx < 0)
+		if (iter->idx == 0)
 			return p;
 		(*pos)--;
 		iter->idx--;

Hi Roel,

Quoted 8 lines

I'm not sure this is the right fix.
If you look at t_next, if there is no more page to look at, iter_idx takes -1.

A 0 value would mean: we are in the first index on the page, which means there is something to read and we don't want to return NULL.

I guess that would be better to turn idx into a signed int.

If we turn idx in a signed int, isn't it true that in kernel/trace/ftrace.c, line 806:

retry:

if (iter->idx >= iter->pg->index) {
	...
} else {
	iter->idx++;
	if ( a certain rec-> and iter->flags )
		goto retry;
}

since iter->pg->index is an unsigned long, when larger than INT_MAX this could result in an endless loop?

Roel

#3 lore raw
Quoted 51 lines · Roel Kluin
Quoted 29 lines · Frederic Weisbecker
Quoted 16 lines · Roel Kluin

// vi kernel/trace/ftrace.c +787
struct ftrace_iterator {
...

        unsigned                idx;

...
};

idx is unsigned and cannot be less than 0.

---

kernel/trace/ftrace.c +0 -0
index 2f32969..a344add 100644

kernel/trace/ftrace.c static void *t_start(struct seq_file *m, loff_t *pos) · line 848

Quoted 8 lines
kernel/trace/ftrace.c +1 -1
@@ -842,7 +842,7 @@ static void *t_start(struct seq_file *m, loff_t *pos)
 	void *p = NULL;
 
 	if (*pos > 0) {
-		if (iter->idx < 0)
+		if (iter->idx == 0)
 			return p;
 		(*pos)--;
 		iter->idx--;

Hi Roel,

Quoted 8 lines

I'm not sure this is the right fix.
If you look at t_next, if there is no more page to look at, iter_idx takes -1.

A 0 value would mean: we are in the first index on the page, which means there is something to read and we don't want to return NULL.

I guess that would be better to turn idx into a signed int.

If we turn idx in a signed int, isn't it true that in kernel/trace/ftrace.c, line 806:

retry:

if (iter->idx >= iter->pg->index) {
	...
} else {
	iter->idx++;
	if ( a certain rec-> and iter->flags )
		goto retry;
}
Quoted 4 lines

since iter->pg->index is an unsigned long, when larger than INT_MAX this could result in an endless loop?

Roel

Actually, this is not supposed to reach such a threshold. Looks like it wouldn't increase over ENTRIES_PER_PAGE (defined in ftrace.c) which is smaller than PAGE_SIZE.
So it will stay far from an overflow.

I don't think this type conversion would be an issue. But perhaps there are other things that I don't see.

author
#4 lore raw

PATCH v2 1/1 ftrace: unsigned idx cannot be less than 0

Quoted 15 lines
Quoted 14 lines
Quoted 11 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-		if (iter->idx < 0)
+		if (iter->idx == 0)

I'm not sure this is the right fix.
If you look at t_next, if there is no more page to look at, iter_idx takes -1.

A 0 value would mean: we are in the first index on the page, which means there is something to read and we don't want to return NULL.

I guess that would be better to turn idx into a signed int.

If we turn idx in a signed int, isn't it true that in kernel/trace/ftrace.c, line 806:

Quoted 7 lines
Quoted 2 lines

since iter->pg->index is an unsigned long, when larger than INT_MAX this could result in an endless loop?

Actually, this is not supposed to reach such a threshold. Looks like it wouldn't increase over ENTRIES_PER_PAGE (defined in ftrace.c) which is smaller than PAGE_SIZE.
So it will stay far from an overflow.

unless signed idx cannot become less than 0

---

kernel/trace/ftrace.c +1 -1
index 2f32969..e256648 100644
@@ -786,7 +786,7 @@ enum {
 
 struct ftrace_iterator {
 	struct ftrace_page	*pg;
-	unsigned		idx;
+	int			idx;
 	unsigned		flags;
 	unsigned char		buffer[FTRACE_BUFF_MAX+1];
 	unsigned		buffer_idx;
maintainer
#5 lore raw
In reply to Frederic Weisbecker

[ added Liming to CC ]

Quoted 29 lines · Frederic Weisbecker
Quoted 16 lines · Roel Kluin

// vi kernel/trace/ftrace.c +787
struct ftrace_iterator {
...

        unsigned                idx;

...
};

idx is unsigned and cannot be less than 0.

---

kernel/trace/ftrace.c +0 -0
index 2f32969..a344add 100644

kernel/trace/ftrace.c static void *t_start(struct seq_file *m, loff_t *pos) · line 848

Quoted 8 lines
kernel/trace/ftrace.c +1 -1
@@ -842,7 +842,7 @@ static void *t_start(struct seq_file *m, loff_t *pos)
 	void *p = NULL;
 
 	if (*pos > 0) {
-		if (iter->idx < 0)
+		if (iter->idx == 0)
 			return p;
 		(*pos)--;
 		iter->idx--;

Hi Roel,

Quoted 8 lines

I'm not sure this is the right fix.
If you look at t_next, if there is no more page to look at, iter_idx takes -1.

A 0 value would mean: we are in the first index on the page, which means there is something to read and we don't want to return NULL.

I guess that would be better to turn idx into a signed int.

Correct. This bug was added by:

50cdaf08a8ec1d7f43987705da7aff7cf949708f
ftrace: improve seq_operation of ftrace

So the correct fix is to turn it into a signed int.

Thanks,

-- Steve

#6 lore raw

2009/1/6 Steven Rostedt <[email protected]>:

Quoted 45 lines

[ added Liming to CC ]

Quoted 29 lines · Frederic Weisbecker
Quoted 16 lines · Roel Kluin

// vi kernel/trace/ftrace.c +787
struct ftrace_iterator {
...

        unsigned                idx;

...
};

idx is unsigned and cannot be less than 0.

---

kernel/trace/ftrace.c +0 -0
index 2f32969..a344add 100644

kernel/trace/ftrace.c static void *t_start(struct seq_file *m, loff_t *pos) · line 848

Quoted 8 lines
kernel/trace/ftrace.c +1 -1
@@ -842,7 +842,7 @@ static void *t_start(struct seq_file *m, loff_t *pos)
    void *p = NULL;
 
    if (*pos > 0) {
-           if (iter->idx < 0)
+           if (iter->idx == 0)
                    return p;
            (*pos)--;
            iter->idx--;

Hi Roel,

Quoted 8 lines

I'm not sure this is the right fix.
If you look at t_next, if there is no more page to look at, iter_idx takes -1.

A 0 value would mean: we are in the first index on the page, which means there is something to read and we don't want to return NULL.

I guess that would be better to turn idx into a signed int.

Correct. This bug was added by:

Quoted 9 lines

50cdaf08a8ec1d7f43987705da7aff7cf949708f
ftrace: improve seq_operation of ftrace

So the correct fix is to turn it into a signed int.

Thanks,

-- Steve

Both iter->idx and pg->index should be signed int I guess.

Roel is perhaps right, I don't know how is caught an unsigned long when compared with a signed int if
this int is less than 0.