Skip to contentSkip to conversation

map_seq_next should increase position index

PATCH In review Vasily Averin 2 participants 6 messages Jan 24, 2020 – Jan 27, 2020 BPF

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 map_seq_next should increase position index

if seq_file .next fuction does not change position index, read after some lseek can generate unexpected output.

https://bugzilla.kernel.org/show_bug.cgi?id=206283

---
 kernel/bpf/inode.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/inode.c +1 -0
index ecf42be..9008a20 100644
@@ -196,6 +196,7 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
 	void *key = map_iter(m)->key;
 	void *prev_key;
 
+	(*pos)++;
 	if (map_iter(m)->done)
 		return NULL;
 
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 15 lines · Vasily Averin

if seq_file .next fuction does not change position index, read after some lseek can generate unexpected output.

https://bugzilla.kernel.org/show_bug.cgi?id=206283

---
  kernel/bpf/inode.c | 1 +
  1 file changed, 1 insertion(+)
kernel/bpf/inode.c +0 -0
index ecf42be..9008a20 100644

kernel/bpf/inode.c static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos) · line 202

Quoted 7 lines
kernel/bpf/inode.c +1 -0
@@ -196,6 +196,7 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
  	void *key = map_iter(m)->key;
  	void *prev_key;
 
+	(*pos)++;
  	if (map_iter(m)->done)
  		return NULL;
 

Hm, how did you test this change? Please elaborate, since in map_seq_next() we do increment position index:

static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)

{
        struct bpf_map *map = seq_file_to_map(m);
        void *key = map_iter(m)->key;
        void *prev_key;

        if (map_iter(m)->done)
                return NULL;

        if (unlikely(v == SEQ_START_TOKEN))
                prev_key = NULL;
        else
                prev_key = key;

        if (map->ops->map_get_next_key(map, prev_key, key)) {
                map_iter(m)->done = true;
                return NULL;
        }

        ++(*pos);                    <------------ here
        return key;
}

With your change we'd increment twice. What is missing here?

Thanks,
Daniel

#2 lore raw
Quoted 10 lines · Daniel Borkmann
Quoted 9 lines · Vasily Averin

if seq_file .next fuction does not change position index, read after some lseek can generate unexpected output.

https://bugzilla.kernel.org/show_bug.cgi?id=206283

---
  kernel/bpf/inode.c | 1 +

  1 file changed, 1 insertion(+)
Quoted 14 lines
Quoted 11 lines
kernel/bpf/inode.c +0 -0
index ecf42be..9008a20 100644
@@ -196,6 +196,7 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
       void *key = map_iter(m)->key;
       void *prev_key;
   +    (*pos)++;
       if (map_iter(m)->done)
           return NULL;

Hm, how did you test this change? Please elaborate, since in map_seq_next() we do increment position index:

Position index should be updated even if .next returns NULL.

Sorry, I've not tested this case.

$ dd if=AFFECTED_FILE bs=1000 skip=1

With any huge bs it will generate last line.
If you'll specify bs in middle of last line --
dd will output rest of list line and then whole last line once again.

Quoted 14 lines

static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos) {
        struct bpf_map *map = seq_file_to_map(m);
        void *key = map_iter(m)->key;
        void *prev_key;

        if (map_iter(m)->done)
                return NULL;

        if (unlikely(v == SEQ_START_TOKEN))
                prev_key = NULL;
        else
                prev_key = key;

Quoted 6 lines

        if (map->ops->map_get_next_key(map, prev_key, key)) { map_iter(m)->done = true;
                return NULL;
        }

        ++(*pos);                    <------------ here

You are right, I've missed it, it should be removed.

Quoted 4 lines

        return key;
}

With your change we'd increment twice. What is missing here?

maintainer
#3 lore raw
Quoted 50 lines · Vasily Averin
Quoted 10 lines · Daniel Borkmann
Quoted 9 lines · Vasily Averin

if seq_file .next fuction does not change position index, read after some lseek can generate unexpected output.

https://bugzilla.kernel.org/show_bug.cgi?id=206283

---
   kernel/bpf/inode.c | 1 +
   1 file changed, 1 insertion(+)
Quoted 14 lines

kernel/bpf/inode.c static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos) · line 201

Quoted 11 lines
kernel/bpf/inode.c +0 -0
index ecf42be..9008a20 100644
@@ -196,6 +196,7 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
       void *key = map_iter(m)->key;
       void *prev_key;
   +    (*pos)++;
       if (map_iter(m)->done)
           return NULL;
 

Hm, how did you test this change? Please elaborate, since in map_seq_next() we do increment position index:

Position index should be updated even if .next returns NULL.

Sorry, I've not tested this case.

$ dd if=AFFECTED_FILE bs=1000 skip=1

With any huge bs it will generate last line.
If you'll specify bs in middle of last line --
dd will output rest of list line and then whole last line once again.

Quoted 14 lines

static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos) {
        struct bpf_map *map = seq_file_to_map(m);
        void *key = map_iter(m)->key;
        void *prev_key;

        if (map_iter(m)->done)
                return NULL;

        if (unlikely(v == SEQ_START_TOKEN))
                prev_key = NULL;
        else
                prev_key = key;

Quoted 8 lines
Quoted 6 lines

        if (map->ops->map_get_next_key(map, prev_key, key)) { map_iter(m)->done = true;
                return NULL;
        }

        ++(*pos);                    <------------ here

You are right, I've missed it, it should be removed.

Ok, please respin v2 then.

Thanks,
Daniel

#4 lore raw

PATCH v2 1/1 map_seq_next should increase position index

v2: removed missed increment in end of function

if seq_file .next fuction does not change position index, read after some lseek can generate unexpected output.

https://bugzilla.kernel.org/show_bug.cgi?id=206283

---
 kernel/bpf/inode.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/bpf/inode.c +1 -2
index ecf42be..6f22e0e 100644
@@ -196,6 +196,7 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
 	void *key = map_iter(m)->key;
 	void *prev_key;
 
+	(*pos)++;
 	if (map_iter(m)->done)
 		return NULL;
 
@@ -208,8 +209,6 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
 		map_iter(m)->done = true;
 		return NULL;
 	}
-
-	++(*pos);
 	return key;
 }
 
Signature · 1 line
1.8.3.1