Skip to contentSkip to conversation

vsprintf: drop comment claiming %n is ignored

PATCH In review Kees Cook 8 participants 22 messages Sep 11, 2013 – Sep 16, 2013

Subsystems (31)
Networking Arm (32-bit) x86 VFS +27 more subsystems
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 vsprintf: drop comment claiming %n is ignored

The %n format is not ignored, so remove the incorrect comment about it.

---
 lib/vsprintf.c |    1 -
 1 file changed, 1 deletion(-)
lib/vsprintf.c +0 -1
index 26559bd..d25d1f8 100644
@@ -1576,7 +1576,6 @@ qualifier:
  *   case.
  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
  *           bytes of the input)
- * %n is ignored
  *
  * ** Please update Documentation/printk-formats.txt when making changes **
  *
Signature · 6 lines
1.7.9.5


-- 
Kees Cook
Chrome OS Security
#1 lore raw
Quoted 1 line · Kees Cook

The %n format is not ignored, so remove the incorrect comment about it.

I think it may be better to reimplement the ignoring.

Maybe:

---
 lib/vsprintf.c      | 18 +++++-------
 net/ipv4/fib_trie.c | 30 +++++++++----------
 net/ipv4/ping.c     | 19 ++++++------
 net/ipv4/tcp_ipv4.c | 84 ++++++++++++++++++++++++++---------------------------
 net/ipv4/udp.c      | 19 ++++++------
 net/phonet/socket.c | 32 ++++++++++----------
 net/sctp/objcnt.c   |  5 ++--
 7 files changed, 99 insertions(+), 108 deletions(-)
lib/vsprintf.c +8 -10
index 26559bd..23f1eb1 100644
@@ -1683,18 +1683,16 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
 			break;
 
 		case FORMAT_TYPE_NRCHARS: {
+			/* skip %n 's argument */
 			u8 qualifier = spec.qualifier;
+			void *skip_arg;
 
-			if (qualifier == 'l') {
-				long *ip = va_arg(args, long *);
-				*ip = (str - buf);
-			} else if (_tolower(qualifier) == 'z') {
-				size_t *ip = va_arg(args, size_t *);
-				*ip = (str - buf);
-			} else {
-				int *ip = va_arg(args, int *);
-				*ip = (str - buf);
-			}
+			if (qualifier == 'l')
+				skip_arg = va_arg(args, long *);
+			else if (_tolower(qualifier) == 'z')
+				skip_arg = va_arg(args, size_t *);
+			else
+				skip_arg = va_arg(args, int *);
 			break;
 		}
 
net/ipv4/fib_trie.c +13 -17
index 3df6d3e..ddf1e9c 100644
@@ -2537,24 +2537,20 @@ static int fib_route_seq_show(struct seq_file *seq, void *v)
 				continue;
 
 			if (fi)
-				seq_printf(seq,
-					 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
-					 "%d\t%08X\t%d\t%u\t%u%n",
-					 fi->fib_dev ? fi->fib_dev->name : "*",
-					 prefix,
-					 fi->fib_nh->nh_gw, flags, 0, 0,
-					 fi->fib_priority,
-					 mask,
-					 (fi->fib_advmss ?
-					  fi->fib_advmss + 40 : 0),
-					 fi->fib_window,
-					 fi->fib_rtt >> 3, &len);
+				len = seq_printf(seq, "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
+						 (fi->fib_dev ?
+						  fi->fib_dev->name : "*"),
+						 prefix,
+						 fi->fib_nh->nh_gw, flags,
+						 0, 0, fi->fib_priority, mask,
+						 (fi->fib_advmss ?
+						  fi->fib_advmss + 40 : 0),
+						 fi->fib_window,
+						 fi->fib_rtt >> 3);
 			else
-				seq_printf(seq,
-					 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
-					 "%d\t%08X\t%d\t%u\t%u%n",
-					 prefix, 0, flags, 0, 0, 0,
-					 mask, 0, 0, 0, &len);
+				len = seq_printf(seq, "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
+						 prefix, 0, flags, 0, 0, 0,
+						 mask, 0, 0, 0);
 
 			seq_printf(seq, "%*s\n", 127 - len, "");
 		}
net/ipv4/ping.c +9 -10
index d7d9882..ac8d79f 100644
@@ -1081,16 +1081,15 @@ static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
 	__u16 destp = ntohs(inet->inet_dport);
 	__u16 srcp = ntohs(inet->inet_sport);
 
-	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
-		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d%n",
-		bucket, src, srcp, dest, destp, sp->sk_state,
-		sk_wmem_alloc_get(sp),
-		sk_rmem_alloc_get(sp),
-		0, 0L, 0,
-		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
-		0, sock_i_ino(sp),
-		atomic_read(&sp->sk_refcnt), sp,
-		atomic_read(&sp->sk_drops), len);
+	*len = seq_printf(f, "%5d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
+			  bucket, src, srcp, dest, destp, sp->sk_state,
+			  sk_wmem_alloc_get(sp),
+			  sk_rmem_alloc_get(sp),
+			  0, 0L, 0,
+			  from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
+			  0, sock_i_ino(sp),
+			  atomic_read(&sp->sk_refcnt), sp,
+			  atomic_read(&sp->sk_drops));
 }
 
 static int ping_v4_seq_show(struct seq_file *seq, void *v)
net/ipv4/tcp_ipv4.c +41 -43

102 lines not on this page · raw message

net/ipv4/udp.c +9 -10
index 74d2c95..ddc24a2d 100644
@@ -2158,16 +2158,15 @@ static void udp4_format_sock(struct sock *sp, struct seq_file *f,
 	__u16 destp	  = ntohs(inet->inet_dport);
 	__u16 srcp	  = ntohs(inet->inet_sport);
 
-	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
-		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d%n",
-		bucket, src, srcp, dest, destp, sp->sk_state,
-		sk_wmem_alloc_get(sp),
-		sk_rmem_alloc_get(sp),
-		0, 0L, 0,
-		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
-		0, sock_i_ino(sp),
-		atomic_read(&sp->sk_refcnt), sp,
-		atomic_read(&sp->sk_drops), len);
+	*len = seq_printf(f, "%5d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
+			  bucket, src, srcp, dest, destp, sp->sk_state,
+			  sk_wmem_alloc_get(sp),
+			  sk_rmem_alloc_get(sp),
+			  0, 0L, 0,
+			  from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
+			  0, sock_i_ino(sp),
+			  atomic_read(&sp->sk_refcnt), sp,
+			  atomic_read(&sp->sk_drops));
 }
 
 int udp4_seq_show(struct seq_file *seq, void *v)
net/phonet/socket.c +16 -16
index 77e38f7..553f896 100644
@@ -598,21 +598,20 @@ static int pn_sock_seq_show(struct seq_file *seq, void *v)
 	int len;
 
 	if (v == SEQ_START_TOKEN)
-		seq_printf(seq, "%s%n", "pt  loc  rem rs st tx_queue rx_queue "
-			"  uid inode ref pointer drops", &len);
+		len = seq_puts(seq, "pt  loc  rem rs st tx_queue rx_queue   uid inode ref pointer drops");
 	else {
 		struct sock *sk = v;
 		struct pn_sock *pn = pn_sk(sk);
 
-		seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
-			"%d %pK %d%n",
-			sk->sk_protocol, pn->sobject, pn->dobject,
-			pn->resource, sk->sk_state,
-			sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
-			from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
-			sock_i_ino(sk),
-			atomic_read(&sk->sk_refcnt), sk,
-			atomic_read(&sk->sk_drops), &len);
+		len = seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu %d %pK %d",
+				 sk->sk_protocol, pn->sobject, pn->dobject,
+				 pn->resource, sk->sk_state,
+				 sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
+				 from_kuid_munged(seq_user_ns(seq),
+						  sock_i_uid(sk)),
+				 sock_i_ino(sk),
+				 atomic_read(&sk->sk_refcnt), sk,
+				 atomic_read(&sk->sk_drops));
 	}
 	seq_printf(seq, "%*s\n", 127 - len, "");
 	return 0;
@@ -788,15 +787,16 @@ static int pn_res_seq_show(struct seq_file *seq, void *v)
 	int len;
 
 	if (v == SEQ_START_TOKEN)
-		seq_printf(seq, "%s%n", "rs   uid inode", &len);
+		len = seq_puts(seq, "rs   uid inode");
 	else {
 		struct sock **psk = v;
 		struct sock *sk = *psk;
 
-		seq_printf(seq, "%02X %5u %lu%n",
-			   (int) (psk - pnres.sk),
-			   from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
-			   sock_i_ino(sk), &len);
+		len = seq_printf(seq, "%02X %5u %lu",
+				 (int) (psk - pnres.sk),
+				 from_kuid_munged(seq_user_ns(seq),
+						  sock_i_uid(sk)),
+				 sock_i_ino(sk));
 	}
 	seq_printf(seq, "%*s\n", 63 - len, "");
 	return 0;
net/sctp/objcnt.c +3 -2
index 5ea573b..8034325 100644
@@ -82,8 +82,9 @@ static int sctp_objcnt_seq_show(struct seq_file *seq, void *v)
 	int i, len;
 
 	i = (int)*(loff_t *)v;
-	seq_printf(seq, "%s: %d%n", sctp_dbg_objcnt[i].label,
-				atomic_read(sctp_dbg_objcnt[i].counter), &len);
+	len = seq_printf(seq, "%s: %d",
+			 sctp_dbg_objcnt[i].label,
+			 atomic_read(sctp_dbg_objcnt[i].counter));
 	seq_printf(seq, "%*s\n", 127 - len, "");
 	return 0;
 }
author
#2 lore raw
Quoted 4 lines · Joe Perches
Quoted 1 line · Kees Cook

The %n format is not ignored, so remove the incorrect comment about it.

I think it may be better to reimplement the ignoring.

Yeah, just had a quick look, and scanf doesn't use this code at all. I'd much rather remove %n again instead.

Quoted 10 lines

Maybe:

---
 lib/vsprintf.c      | 18 +++++-------
 net/ipv4/fib_trie.c | 30 +++++++++----------
 net/ipv4/ping.c     | 19 ++++++------
 net/ipv4/tcp_ipv4.c | 84 ++++++++++++++++++++++++++---------------------------
 net/ipv4/udp.c      | 19 ++++++------
 net/phonet/socket.c | 32 ++++++++++----------
 net/sctp/objcnt.c   |  5 ++--
 7 files changed, 99 insertions(+), 108 deletions(-)

This needs fs/proc/consoles.c, nommu.c, task_mmu.c, and task_nommu.c added too, if I can believe my "git grep".

Quoted 19 lines
lib/vsprintf.c +2 -6
index 26559bd..23f1eb1 100644
@@ -1683,18 +1683,16 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
                        break;
 
                case FORMAT_TYPE_NRCHARS: {
+                       /* skip %n 's argument */
                        u8 qualifier = spec.qualifier;
+                       void *skip_arg;
 
-                       if (qualifier == 'l') {
-                               long *ip = va_arg(args, long *);
-                               *ip = (str - buf);
-                       } else if (_tolower(qualifier) == 'z') {
-                               size_t *ip = va_arg(args, size_t *);
-                               *ip = (str - buf);

lib/vsprintf.c int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) · line 1695

Quoted 10 lines
lib/vsprintf.c +6 -4
-                       } else {
-                               int *ip = va_arg(args, int *);
-                               *ip = (str - buf);
-                       }
+                       if (qualifier == 'l')
+                               skip_arg = va_arg(args, long *);
+                       else if (_tolower(qualifier) == 'z')
+                               skip_arg = va_arg(args, size_t *);
+                       else
+                               skip_arg = va_arg(args, int *);

I think Dan's idea of a WARN_ON_ONCE() here is a good idea to help anyone accidentally trying to use it.

-Kees

Quoted 290 lines
lib/vsprintf.c +0 -0
                        break;
                }
 
net/ipv4/fib_trie.c +13 -17
index 3df6d3e..ddf1e9c 100644
@@ -2537,24 +2537,20 @@ static int fib_route_seq_show(struct seq_file *seq, void *v)
                                continue;
 
                        if (fi)
-                               seq_printf(seq,
-                                        "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
-                                        "%d\t%08X\t%d\t%u\t%u%n",
-                                        fi->fib_dev ? fi->fib_dev->name : "*",
-                                        prefix,
-                                        fi->fib_nh->nh_gw, flags, 0, 0,
-                                        fi->fib_priority,
-                                        mask,
-                                        (fi->fib_advmss ?
-                                         fi->fib_advmss + 40 : 0),
-                                        fi->fib_window,
-                                        fi->fib_rtt >> 3, &len);
+                               len = seq_printf(seq, "%s\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
+                                                (fi->fib_dev ?
+                                                 fi->fib_dev->name : "*"),
+                                                prefix,
+                                                fi->fib_nh->nh_gw, flags,
+                                                0, 0, fi->fib_priority, mask,
+                                                (fi->fib_advmss ?
+                                                 fi->fib_advmss + 40 : 0),
+                                                fi->fib_window,
+                                                fi->fib_rtt >> 3);
                        else
-                               seq_printf(seq,
-                                        "*\t%08X\t%08X\t%04X\t%d\t%u\t"
-                                        "%d\t%08X\t%d\t%u\t%u%n",
-                                        prefix, 0, flags, 0, 0, 0,
-                                        mask, 0, 0, 0, &len);
+                               len = seq_printf(seq, "*\t%08X\t%08X\t%04X\t%d\t%u\t%d\t%08X\t%d\t%u\t%u",
+                                                prefix, 0, flags, 0, 0, 0,
+                                                mask, 0, 0, 0);
 
                        seq_printf(seq, "%*s\n", 127 - len, "");
                }
net/ipv4/ping.c +9 -10
index d7d9882..ac8d79f 100644
@@ -1081,16 +1081,15 @@ static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
        __u16 destp = ntohs(inet->inet_dport);
        __u16 srcp = ntohs(inet->inet_sport);
 
-       seq_printf(f, "%5d: %08X:%04X %08X:%04X"
-               " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d%n",
-               bucket, src, srcp, dest, destp, sp->sk_state,
-               sk_wmem_alloc_get(sp),
-               sk_rmem_alloc_get(sp),
-               0, 0L, 0,
-               from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
-               0, sock_i_ino(sp),
-               atomic_read(&sp->sk_refcnt), sp,
-               atomic_read(&sp->sk_drops), len);
+       *len = seq_printf(f, "%5d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
+                         bucket, src, srcp, dest, destp, sp->sk_state,
+                         sk_wmem_alloc_get(sp),
+                         sk_rmem_alloc_get(sp),
+                         0, 0L, 0,
+                         from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
+                         0, sock_i_ino(sp),
+                         atomic_read(&sp->sk_refcnt), sp,
+                         atomic_read(&sp->sk_drops));
 }
 
 static int ping_v4_seq_show(struct seq_file *seq, void *v)
net/ipv4/tcp_ipv4.c +41 -43
index b14266b..1279c16 100644
@@ -2603,24 +2603,24 @@ static void get_openreq4(const struct sock *sk, const struct request_sock *req,
        const struct inet_request_sock *ireq = inet_rsk(req);
        long delta = req->expires - jiffies;
 
-       seq_printf(f, "%4d: %08X:%04X %08X:%04X"
-               " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK%n",
-               i,
-               ireq->loc_addr,
-               ntohs(inet_sk(sk)->inet_sport),
-               ireq->rmt_addr,
-               ntohs(ireq->rmt_port),
-               TCP_SYN_RECV,
-               0, 0, /* could print option size, but that is af dependent. */
-               1,    /* timers active (only the expire timer) */
-               jiffies_delta_to_clock_t(delta),
-               req->num_timeout,
-               from_kuid_munged(seq_user_ns(f), uid),
-               0,  /* non standard timer */
-               0, /* open_requests have no inode */
-               atomic_read(&sk->sk_refcnt),
-               req,
-               len);
+       *len = seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK",
+                         i,
+                         ireq->loc_addr,
+                         ntohs(inet_sk(sk)->inet_sport),
+                         ireq->rmt_addr,
+                         ntohs(ireq->rmt_port),
+                         TCP_SYN_RECV,
+                         0, 0, /* could print option size,
+                                * but that is af dependent.
+                                */
+                         1,    /* timers active (only the expire timer) */
+                         jiffies_delta_to_clock_t(delta),
+                         req->num_timeout,
+                         from_kuid_munged(seq_user_ns(f), uid),
+                         0,    /* non standard timer */
+                         0,    /* open_requests have no inode */
+                         atomic_read(&sk->sk_refcnt),
+                         req);
 }
 
 static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
@@ -2661,26 +2661,25 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
                 */
                rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
 
-       seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
-                       "%08X %5u %8d %lu %d %pK %lu %lu %u %u %d%n",
-               i, src, srcp, dest, destp, sk->sk_state,
-               tp->write_seq - tp->snd_una,
-               rx_queue,
-               timer_active,
-               jiffies_delta_to_clock_t(timer_expires - jiffies),
-               icsk->icsk_retransmits,
-               from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)),
-               icsk->icsk_probes_out,
-               sock_i_ino(sk),
-               atomic_read(&sk->sk_refcnt), sk,
-               jiffies_to_clock_t(icsk->icsk_rto),
-               jiffies_to_clock_t(icsk->icsk_ack.ato),
-               (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
-               tp->snd_cwnd,
-               sk->sk_state == TCP_LISTEN ?
-                   (fastopenq ? fastopenq->max_qlen : 0) :
-                   (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh),
-               len);
+       *len = seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %lu %lu %u %u %d",
+                         i, src, srcp, dest, destp, sk->sk_state,
+                         tp->write_seq - tp->snd_una,
+                         rx_queue,
+                         timer_active,
+                         jiffies_delta_to_clock_t(timer_expires - jiffies),
+                         icsk->icsk_retransmits,
+                         from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)),
+                         icsk->icsk_probes_out,
+                         sock_i_ino(sk),
+                         atomic_read(&sk->sk_refcnt), sk,
+                         jiffies_to_clock_t(icsk->icsk_rto),
+                         jiffies_to_clock_t(icsk->icsk_ack.ato),
+                         (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
+                         tp->snd_cwnd,
+                         sk->sk_state == TCP_LISTEN ?
+                         (fastopenq ? fastopenq->max_qlen : 0) :
+                         (tcp_in_initial_slowstart(tp) ? -1 :
+                          tp->snd_ssthresh));
 }
 
 static void get_timewait4_sock(const struct inet_timewait_sock *tw,
@@ -2695,11 +2694,10 @@ static void get_timewait4_sock(const struct inet_timewait_sock *tw,
        destp = ntohs(tw->tw_dport);
        srcp  = ntohs(tw->tw_sport);
 
-       seq_printf(f, "%4d: %08X:%04X %08X:%04X"
-               " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK%n",
-               i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
-               3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
-               atomic_read(&tw->tw_refcnt), tw, len);
+       *len = seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK",
+                         i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
+                         3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
+                         atomic_read(&tw->tw_refcnt), tw);
 }
 
 #define TMPSZ 150
net/ipv4/udp.c +9 -10
index 74d2c95..ddc24a2d 100644
@@ -2158,16 +2158,15 @@ static void udp4_format_sock(struct sock *sp, struct seq_file *f,
        __u16 destp       = ntohs(inet->inet_dport);
        __u16 srcp        = ntohs(inet->inet_sport);
 
-       seq_printf(f, "%5d: %08X:%04X %08X:%04X"
-               " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d%n",
-               bucket, src, srcp, dest, destp, sp->sk_state,
-               sk_wmem_alloc_get(sp),
-               sk_rmem_alloc_get(sp),
-               0, 0L, 0,
-               from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
-               0, sock_i_ino(sp),
-               atomic_read(&sp->sk_refcnt), sp,
-               atomic_read(&sp->sk_drops), len);
+       *len = seq_printf(f, "%5d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
+                         bucket, src, srcp, dest, destp, sp->sk_state,
+                         sk_wmem_alloc_get(sp),
+                         sk_rmem_alloc_get(sp),
+                         0, 0L, 0,
+                         from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
+                         0, sock_i_ino(sp),
+                         atomic_read(&sp->sk_refcnt), sp,
+                         atomic_read(&sp->sk_drops));
 }
 
 int udp4_seq_show(struct seq_file *seq, void *v)
net/phonet/socket.c +16 -16
index 77e38f7..553f896 100644
@@ -598,21 +598,20 @@ static int pn_sock_seq_show(struct seq_file *seq, void *v)
        int len;
 
        if (v == SEQ_START_TOKEN)
-               seq_printf(seq, "%s%n", "pt  loc  rem rs st tx_queue rx_queue "
-                       "  uid inode ref pointer drops", &len);
+               len = seq_puts(seq, "pt  loc  rem rs st tx_queue rx_queue   uid inode ref pointer drops");
        else {
                struct sock *sk = v;
                struct pn_sock *pn = pn_sk(sk);
 
-               seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
-                       "%d %pK %d%n",
-                       sk->sk_protocol, pn->sobject, pn->dobject,
-                       pn->resource, sk->sk_state,
-                       sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
-                       from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
-                       sock_i_ino(sk),
-                       atomic_read(&sk->sk_refcnt), sk,
-                       atomic_read(&sk->sk_drops), &len);
+               len = seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu %d %pK %d",
+                                sk->sk_protocol, pn->sobject, pn->dobject,
+                                pn->resource, sk->sk_state,
+                                sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
+                                from_kuid_munged(seq_user_ns(seq),
+                                                 sock_i_uid(sk)),
+                                sock_i_ino(sk),
+                                atomic_read(&sk->sk_refcnt), sk,
+                                atomic_read(&sk->sk_drops));
        }
        seq_printf(seq, "%*s\n", 127 - len, "");
        return 0;
@@ -788,15 +787,16 @@ static int pn_res_seq_show(struct seq_file *seq, void *v)
        int len;
 
        if (v == SEQ_START_TOKEN)
-               seq_printf(seq, "%s%n", "rs   uid inode", &len);
+               len = seq_puts(seq, "rs   uid inode");
        else {
                struct sock **psk = v;
                struct sock *sk = *psk;
 
-               seq_printf(seq, "%02X %5u %lu%n",
-                          (int) (psk - pnres.sk),
-                          from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
-                          sock_i_ino(sk), &len);
+               len = seq_printf(seq, "%02X %5u %lu",
+                                (int) (psk - pnres.sk),
+                                from_kuid_munged(seq_user_ns(seq),
+                                                 sock_i_uid(sk)),
+                                sock_i_ino(sk));
        }
        seq_printf(seq, "%*s\n", 63 - len, "");
        return 0;
net/sctp/objcnt.c +3 -2
index 5ea573b..8034325 100644
@@ -82,8 +82,9 @@ static int sctp_objcnt_seq_show(struct seq_file *seq, void *v)
        int i, len;
 
        i = (int)*(loff_t *)v;
-       seq_printf(seq, "%s: %d%n", sctp_dbg_objcnt[i].label,
-                               atomic_read(sctp_dbg_objcnt[i].counter), &len);
+       len = seq_printf(seq, "%s: %d",
+                        sctp_dbg_objcnt[i].label,
+                        atomic_read(sctp_dbg_objcnt[i].counter));
        seq_printf(seq, "%*s\n", 127 - len, "");
        return 0;
 }
Signature · 2 lines
Kees Cook
Chrome OS Security
#3 lore raw
Quoted 20 lines · Kees Cook
Quoted 4 lines · Joe Perches
Quoted 1 line · Kees Cook

The %n format is not ignored, so remove the incorrect comment about it.

I think it may be better to reimplement the ignoring.

Yeah, just had a quick look, and scanf doesn't use this code at all. I'd much rather remove %n again instead.

Quoted 10 lines

Maybe:

---
 lib/vsprintf.c      | 18 +++++-------
 net/ipv4/fib_trie.c | 30 +++++++++----------
 net/ipv4/ping.c     | 19 ++++++------
 net/ipv4/tcp_ipv4.c | 84 ++++++++++++++++++++++++++---------------------------
 net/ipv4/udp.c      | 19 ++++++------
 net/phonet/socket.c | 32 ++++++++++----------
 net/sctp/objcnt.c   |  5 ++--
 7 files changed, 99 insertions(+), 108 deletions(-)
Quoted 2 lines

This needs fs/proc/consoles.c, nommu.c, task_mmu.c, and task_nommu.c added too, if I can believe my "git grep".

Yeah, just found those too.

---
 fs/proc/consoles.c   |  2 +-
 fs/proc/nommu.c      | 20 ++++++++++----------
 fs/proc/task_mmu.c   | 18 +++++++++---------
 fs/proc/task_nommu.c | 20 ++++++++++----------
 4 files changed, 30 insertions(+), 30 deletions(-)
fs/proc/consoles.c +1 -1
index b701eaa..42f2bb7 100644
@@ -47,7 +47,7 @@ static int show_console_dev(struct seq_file *m, void *v)
 			con_flags[a].name : ' ';
 	flags[a] = 0;
 
-	seq_printf(m, "%s%d%n", con->name, con->index, &len);
+	len = seq_printf(m, "%s%d", con->name, con->index);
 	len = 21 - len;
 	if (len < 1)
 		len = 1;
fs/proc/nommu.c +10 -10
index ccfd99b..91cfd19 100644
@@ -50,16 +50,16 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
 		ino = inode->i_ino;
 	}
 
-	seq_printf(m,
-		   "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
-		   region->vm_start,
-		   region->vm_end,
-		   flags & VM_READ ? 'r' : '-',
-		   flags & VM_WRITE ? 'w' : '-',
-		   flags & VM_EXEC ? 'x' : '-',
-		   flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
-		   ((loff_t)region->vm_pgoff) << PAGE_SHIFT,
-		   MAJOR(dev), MINOR(dev), ino, &len);
+	len = seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
+			 region->vm_start,
+			 region->vm_end,
+			 flags & VM_READ ? 'r' : '-',
+			 flags & VM_WRITE ? 'w' : '-',
+			 flags & VM_EXEC ? 'x' : '-',
+			 flags & VM_MAYSHARE ?
+			 flags & VM_SHARED ? 'S' : 's' : 'p',
+			 ((loff_t)region->vm_pgoff) << PAGE_SHIFT,
+			 MAJOR(dev), MINOR(dev), ino);
 
 	if (file) {
 		len = 25 + sizeof(void *) * 6 - len;
fs/proc/task_mmu.c +9 -9
index 107d026..f84ee9f 100644
@@ -286,15 +286,15 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
 	if (stack_guard_page_end(vma, end))
 		end -= PAGE_SIZE;
 
-	seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
-			start,
-			end,
-			flags & VM_READ ? 'r' : '-',
-			flags & VM_WRITE ? 'w' : '-',
-			flags & VM_EXEC ? 'x' : '-',
-			flags & VM_MAYSHARE ? 's' : 'p',
-			pgoff,
-			MAJOR(dev), MINOR(dev), ino, &len);
+	len = seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
+			 start,
+			 end,
+			 flags & VM_READ ? 'r' : '-',
+			 flags & VM_WRITE ? 'w' : '-',
+			 flags & VM_EXEC ? 'x' : '-',
+			 flags & VM_MAYSHARE ? 's' : 'p',
+			 pgoff,
+			 MAJOR(dev), MINOR(dev), ino);
 
 	/*
 	 * Print the dentry name for named mappings, and a
fs/proc/task_nommu.c +10 -10
index 56123a6..1d7bbe5 100644
@@ -155,16 +155,16 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
 		pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
 	}
 
-	seq_printf(m,
-		   "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
-		   vma->vm_start,
-		   vma->vm_end,
-		   flags & VM_READ ? 'r' : '-',
-		   flags & VM_WRITE ? 'w' : '-',
-		   flags & VM_EXEC ? 'x' : '-',
-		   flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
-		   pgoff,
-		   MAJOR(dev), MINOR(dev), ino, &len);
+	len = seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
+			 vma->vm_start,
+			 vma->vm_end,
+			 flags & VM_READ ? 'r' : '-',
+			 flags & VM_WRITE ? 'w' : '-',
+			 flags & VM_EXEC ? 'x' : '-',
+			 flags & VM_MAYSHARE ?
+			 flags & VM_SHARED ? 'S' : 's' : 'p',
+			 pgoff,
+			 MAJOR(dev), MINOR(dev), ino);
 
 	if (file) {
 		pad_len_spaces(m, len);
#4 lore raw
Quoted 102 lines

Yeah, just found those too.

---
  fs/proc/consoles.c   |  2 +-
  fs/proc/nommu.c      | 20 ++++++++++----------
  fs/proc/task_mmu.c   | 18 +++++++++---------
  fs/proc/task_nommu.c | 20 ++++++++++----------
  4 files changed, 30 insertions(+), 30 deletions(-)
fs/proc/consoles.c +1 -1
index b701eaa..42f2bb7 100644
@@ -47,7 +47,7 @@ static int show_console_dev(struct seq_file *m, void *v)
  			con_flags[a].name : ' ';
  	flags[a] = 0;
 
-	seq_printf(m, "%s%d%n", con->name, con->index, &len);
+	len = seq_printf(m, "%s%d", con->name, con->index);
  	len = 21 - len;
  	if (len < 1)
  		len = 1;
fs/proc/nommu.c +10 -10
index ccfd99b..91cfd19 100644
@@ -50,16 +50,16 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
  		ino = inode->i_ino;
  	}
 
-	seq_printf(m,
-		   "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
-		   region->vm_start,
-		   region->vm_end,
-		   flags & VM_READ ? 'r' : '-',
-		   flags & VM_WRITE ? 'w' : '-',
-		   flags & VM_EXEC ? 'x' : '-',
-		   flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
-		   ((loff_t)region->vm_pgoff) << PAGE_SHIFT,
-		   MAJOR(dev), MINOR(dev), ino, &len);
+	len = seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
+			 region->vm_start,
+			 region->vm_end,
+			 flags & VM_READ ? 'r' : '-',
+			 flags & VM_WRITE ? 'w' : '-',
+			 flags & VM_EXEC ? 'x' : '-',
+			 flags & VM_MAYSHARE ?
+			 flags & VM_SHARED ? 'S' : 's' : 'p',
+			 ((loff_t)region->vm_pgoff) << PAGE_SHIFT,
+			 MAJOR(dev), MINOR(dev), ino);
 
  	if (file) {
  		len = 25 + sizeof(void *) * 6 - len;
fs/proc/task_mmu.c +9 -9
index 107d026..f84ee9f 100644
@@ -286,15 +286,15 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
  	if (stack_guard_page_end(vma, end))
  		end -= PAGE_SIZE;
 
-	seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
-			start,
-			end,
-			flags & VM_READ ? 'r' : '-',
-			flags & VM_WRITE ? 'w' : '-',
-			flags & VM_EXEC ? 'x' : '-',
-			flags & VM_MAYSHARE ? 's' : 'p',
-			pgoff,
-			MAJOR(dev), MINOR(dev), ino, &len);
+	len = seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
+			 start,
+			 end,
+			 flags & VM_READ ? 'r' : '-',
+			 flags & VM_WRITE ? 'w' : '-',
+			 flags & VM_EXEC ? 'x' : '-',
+			 flags & VM_MAYSHARE ? 's' : 'p',
+			 pgoff,
+			 MAJOR(dev), MINOR(dev), ino);
 
  	/*
  	 * Print the dentry name for named mappings, and a
fs/proc/task_nommu.c +3 -10
index 56123a6..1d7bbe5 100644
@@ -155,16 +155,16 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
  		pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  	}
 
-	seq_printf(m,
-		   "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
-		   vma->vm_start,
-		   vma->vm_end,
-		   flags & VM_READ ? 'r' : '-',
-		   flags & VM_WRITE ? 'w' : '-',
-		   flags & VM_EXEC ? 'x' : '-',
-		   flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
-		   pgoff,
-		   MAJOR(dev), MINOR(dev), ino, &len);
+	len = seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
+			 vma->vm_start,
+			 vma->vm_end,

fs/proc/task_nommu.c static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma, · line 170

Quoted 10 lines
fs/proc/task_nommu.c +7 -0
+			 flags & VM_READ ? 'r' : '-',
+			 flags & VM_WRITE ? 'w' : '-',
+			 flags & VM_EXEC ? 'x' : '-',
+			 flags & VM_MAYSHARE ?
+			 flags & VM_SHARED ? 'S' : 's' : 'p',
+			 pgoff,
+			 MAJOR(dev), MINOR(dev), ino);
 
  	if (file) {
  		pad_len_spaces(m, len);

ack for mm parts.

#5 lore raw
In reply to Kees Cook
Quoted 2 lines · Kees Cook

I think Dan's idea of a WARN_ON_ONCE() here is a good idea to help anyone accidentally trying to use it.

Maybe WARN_ONCE so it's easier to emit the format too.

#6 lore raw
Quoted 1 line

Maybe WARN_ONCE so it's easier to emit the format too.

Good idea. And, if it's not too much trouble, a comment explaining why it's deliberately omitted so the issue doesn't arise again.

#7 lore raw
Quoted 4 lines · George Spelvin
Quoted 1 line

Maybe WARN_ONCE so it's easier to emit the format too.

Good idea. And, if it's not too much trouble, a comment explaining why it's deliberately omitted so the issue doesn't arise again.

Before any of the %n uses could be removed, I believe seq_printf could to be converted to return void and have a another mechanism to determine if any error occurred and the length of the output of seq_printf.

I've done a preliminary conversion of seq_printf and seq_vprintf to return void and added last_ret and last_len to struct seq_file.

If that's applied, it's trivial to convert vsnprintf to skip %n.

Anyone have an opinion of a different conversion mechanism?

Converting all the uses was (mostly) done with coccinelle.

Only compiled x86-32, several files uncompiled.
Completely untested.

---

 arch/arm/plat-pxa/dma.c                            |  95 +++++++------
 arch/microblaze/kernel/cpu/mb.c                    | 145 ++++++++++----------
 arch/x86/kernel/cpu/mtrr/if.c                      |   9 +-
 drivers/base/power/wakeup.c                        |  14 +-
 drivers/mfd/ab8500-debugfs.c                       |  12 +-
 drivers/mtd/devices/docg3.c                        |  97 ++++++-------
 drivers/parisc/ccio-dma.c                          |  52 +++----
 drivers/parisc/sba_iommu.c                         |  74 +++++-----
 drivers/regulator/dbx500-prcmu.c                   |  34 ++---
 drivers/staging/lustre/lustre/fid/lproc_fid.c      |  16 ++-
 drivers/staging/lustre/lustre/llite/lproc_llite.c  |  32 +++--
 drivers/staging/lustre/lustre/mdc/lproc_mdc.c      |   3 +-
 .../lustre/lustre/obdclass/lprocfs_status.c        |  64 +++++----
 drivers/staging/lustre/lustre/osc/lproc_osc.c      |  28 ++--
 .../staging/lustre/lustre/ptlrpc/lproc_ptlrpc.c    |   3 +-
 drivers/staging/lustre/lustre/ptlrpc/sec_bulk.c    |  78 +++++------
 drivers/usb/gadget/pxa27x_udc.c                    | 108 +++++++--------
 fs/debugfs/file.c                                  |   6 +-
 fs/dlm/debug_fs.c                                  | 152 +++++++++++----------
 fs/eventfd.c                                       |   5 +-
 fs/eventpoll.c                                     |   7 +-
 fs/notify/fdinfo.c                                 |  34 ++---
 fs/seq_file.c                                      |  15 +-
 include/linux/seq_file.h                           |   6 +-
 ipc/util.c                                         |   6 +-
 net/batman-adv/gateway_client.c                    |  21 +--
 .../netfilter/nf_conntrack_l3proto_ipv4_compat.c   |  30 ++--
 net/netfilter/nf_conntrack_standalone.c            |  35 +++--
 net/netfilter/nf_log.c                             |  12 +-
 net/netfilter/xt_hashlimit.c                       |  34 ++---
 30 files changed, 649 insertions(+), 578 deletions(-)
arch/arm/plat-pxa/dma.c +46 -49
index 79ef102..f2c5e34 100644
@@ -56,12 +56,12 @@ static int dbg_show_requester_chan(struct seq_file *s, void *p)
 	int i;
 	u32 drcmr;
 
-	pos += seq_printf(s, "DMA channel %d requesters list :\n", chan);
+	seq_printf(s, "DMA channel %d requesters list :\n", chan);
 	for (i = 0; i < DMA_MAX_REQUESTERS; i++) {
 		drcmr = DRCMR(i);
 		if ((drcmr & DRCMR_CHLNUM) == chan)
-			pos += seq_printf(s, "\tRequester %d (MAPVLD=%d)\n", i,
-					  !!(drcmr & DRCMR_MAPVLD));
+			seq_printf(s, "\tRequester %d (MAPVLD=%d)\n",
+				   i, !!(drcmr & DRCMR_MAPVLD));
 	}
 	return pos;
 }
@@ -94,36 +94,35 @@ static int dbg_show_descriptors(struct seq_file *s, void *p)
 	spin_lock_irqsave(&dma_channels[chan].lock, flags);
 	phys_desc = DDADR(chan);
 
-	pos += seq_printf(s, "DMA channel %d descriptors :\n", chan);
-	pos += seq_printf(s, "[%03d] First descriptor unknown\n", 0);
+	seq_printf(s, "DMA channel %d descriptors :\n", chan);
+	seq_printf(s, "[%03d] First descriptor unknown\n", 0);
 	for (i = 1; i < max_show && is_phys_valid(phys_desc); i++) {
 		desc = phys_to_virt(phys_desc);
 		dcmd = desc->dcmd;
 		burst = dbg_burst_from_dcmd(dcmd);
 		width = (1 << ((dcmd >> 14) & 0x3)) >> 1;
 
-		pos += seq_printf(s, "[%03d] Desc at %08lx(virt %p)\n",
-				  i, phys_desc, desc);
-		pos += seq_printf(s, "\tDDADR = %08x\n", desc->ddadr);
-		pos += seq_printf(s, "\tDSADR = %08x\n", desc->dsadr);
-		pos += seq_printf(s, "\tDTADR = %08x\n", desc->dtadr);
-		pos += seq_printf(s, "\tDCMD  = %08x (%s%s%s%s%s%s%sburst=%d"
-				  " width=%d len=%d)\n",
-				  dcmd,
-				  DCMD_STR(INCSRCADDR), DCMD_STR(INCTRGADDR),
-				  DCMD_STR(FLOWSRC), DCMD_STR(FLOWTRG),
-				  DCMD_STR(STARTIRQEN), DCMD_STR(ENDIRQEN),
-				  DCMD_STR(ENDIAN), burst, width,
-				  dcmd & DCMD_LENGTH);
+		seq_printf(s, "[%03d] Desc at %08lx(virt %p)\n",
+			   i, phys_desc, desc);
+		seq_printf(s, "\tDDADR = %08x\n", desc->ddadr);
+		seq_printf(s, "\tDSADR = %08x\n", desc->dsadr);
+		seq_printf(s, "\tDTADR = %08x\n", desc->dtadr);
+		seq_printf(s, "\tDCMD  = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
+			   dcmd,
+			   DCMD_STR(INCSRCADDR), DCMD_STR(INCTRGADDR),
+			   DCMD_STR(FLOWSRC), DCMD_STR(FLOWTRG),
+			   DCMD_STR(STARTIRQEN), DCMD_STR(ENDIRQEN),
+			   DCMD_STR(ENDIAN), burst, width,
+			   dcmd & DCMD_LENGTH);
 		phys_desc = desc->ddadr;
 	}
 	if (i == max_show)
-		pos += seq_printf(s, "[%03d] Desc at %08lx ... max display reached\n",
-				  i, phys_desc);
+		seq_printf(s, "[%03d] Desc at %08lx ... max display reached\n",
+			   i, phys_desc);
 	else
-		pos += seq_printf(s, "[%03d] Desc at %08lx is %s\n",
-				  i, phys_desc, phys_desc == DDADR_STOP ?
-				  "DDADR_STOP" : "invalid");
+		seq_printf(s, "[%03d] Desc at %08lx is %s\n",
+			   i, phys_desc, phys_desc == DDADR_STOP ?
+			   "DDADR_STOP" : "invalid");
 
 	spin_unlock_irqrestore(&dma_channels[chan].lock, flags);
 	return pos;
@@ -142,30 +141,28 @@ static int dbg_show_chan_state(struct seq_file *s, void *p)
 	burst = dbg_burst_from_dcmd(dcmd);
 	width = (1 << ((dcmd >> 14) & 0x3)) >> 1;
 
-	pos += seq_printf(s, "DMA channel %d\n", chan);
-	pos += seq_printf(s, "\tPriority : %s\n",
-			  str_prio[dma_channels[chan].prio]);
-	pos += seq_printf(s, "\tUnaligned transfer bit: %s\n",
-			  DALGN & (1 << chan) ? "yes" : "no");
-	pos += seq_printf(s, "\tDCSR  = %08x (%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
-			  dcsr, DCSR_STR(RUN), DCSR_STR(NODESC),
-			  DCSR_STR(STOPIRQEN), DCSR_STR(EORIRQEN),
-			  DCSR_STR(EORJMPEN), DCSR_STR(EORSTOPEN),
-			  DCSR_STR(SETCMPST), DCSR_STR(CLRCMPST),
-			  DCSR_STR(CMPST), DCSR_STR(EORINTR), DCSR_STR(REQPEND),
-			  DCSR_STR(STOPSTATE), DCSR_STR(ENDINTR),
-			  DCSR_STR(STARTINTR), DCSR_STR(BUSERR));
-
-	pos += seq_printf(s, "\tDCMD  = %08x (%s%s%s%s%s%s%sburst=%d width=%d"
-			  " len=%d)\n",
-			  dcmd,
-			  DCMD_STR(INCSRCADDR), DCMD_STR(INCTRGADDR),
-			  DCMD_STR(FLOWSRC), DCMD_STR(FLOWTRG),
-			  DCMD_STR(STARTIRQEN), DCMD_STR(ENDIRQEN),
-			  DCMD_STR(ENDIAN), burst, width, dcmd & DCMD_LENGTH);
-	pos += seq_printf(s, "\tDSADR = %08x\n", DSADR(chan));
-	pos += seq_printf(s, "\tDTADR = %08x\n", DTADR(chan));
-	pos += seq_printf(s, "\tDDADR = %08x\n", DDADR(chan));
+	seq_printf(s, "DMA channel %d\n", chan);
+	seq_printf(s, "\tPriority : %s\n", str_prio[dma_channels[chan].prio]);
+	seq_printf(s, "\tUnaligned transfer bit: %s\n",
+		   DALGN & (1 << chan) ? "yes" : "no");
+	seq_printf(s, "\tDCSR  = %08x (%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
+		   dcsr, DCSR_STR(RUN), DCSR_STR(NODESC),
+		   DCSR_STR(STOPIRQEN), DCSR_STR(EORIRQEN),
+		   DCSR_STR(EORJMPEN), DCSR_STR(EORSTOPEN),
+		   DCSR_STR(SETCMPST), DCSR_STR(CLRCMPST),
+		   DCSR_STR(CMPST), DCSR_STR(EORINTR), DCSR_STR(REQPEND),
+		   DCSR_STR(STOPSTATE), DCSR_STR(ENDINTR),
+		   DCSR_STR(STARTINTR), DCSR_STR(BUSERR));
+
+	seq_printf(s, "\tDCMD  = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
+		   dcmd,
+		   DCMD_STR(INCSRCADDR), DCMD_STR(INCTRGADDR),
+		   DCMD_STR(FLOWSRC), DCMD_STR(FLOWTRG),
+		   DCMD_STR(STARTIRQEN), DCMD_STR(ENDIRQEN),
+		   DCMD_STR(ENDIAN), burst, width, dcmd & DCMD_LENGTH);
+	seq_printf(s, "\tDSADR = %08x\n", DSADR(chan));
+	seq_printf(s, "\tDTADR = %08x\n", DTADR(chan));
+	seq_printf(s, "\tDDADR = %08x\n", DDADR(chan));
 	return pos;
 }
 
@@ -174,8 +171,8 @@ static int dbg_show_state(struct seq_file *s, void *p)
 	int pos = 0;
 
 	/* basic device status */
-	pos += seq_printf(s, "DMA engine status\n");
-	pos += seq_printf(s, "\tChannel number: %d\n", num_dma_channels);
+	seq_puts(s, "DMA engine status\n");
+	seq_printf(s, "\tChannel number: %d\n", num_dma_channels);
 
 	return pos;
 }
arch/microblaze/kernel/cpu/mb.c +72 -73

163 lines not on this page · raw message

arch/x86/kernel/cpu/mtrr/if.c +4 -5
index a041e09..8513d2e 100644
@@ -425,11 +425,10 @@ static int mtrr_seq_show(struct seq_file *seq, void *offset)
 			size >>= 20 - PAGE_SHIFT;
 		}
 		/* Base can be > 32bit */
-		len += seq_printf(seq, "reg%02i: base=0x%06lx000 "
-			"(%5luMB), size=%5lu%cB, count=%d: %s\n",
-			i, base, base >> (20 - PAGE_SHIFT), size,
-			factor, mtrr_usage_table[i],
-			mtrr_attrib_to_str(type));
+		seq_printf(seq, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
+			   i, base, base >> (20 - PAGE_SHIFT), size,
+			   factor, mtrr_usage_table[i],
+			   mtrr_attrib_to_str(type));
 	}
 	return 0;
 }
drivers/base/power/wakeup.c +7 -7
index 2d56f41..6568d2d 100644
@@ -845,13 +845,13 @@ static int print_wakeup_source_stats(struct seq_file *m,
 		active_time = ktime_set(0, 0);
 	}
 
-	ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t"
-			"%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n",
-			ws->name, active_count, ws->event_count,
-			ws->wakeup_count, ws->expire_count,
-			ktime_to_ms(active_time), ktime_to_ms(total_time),
-			ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
-			ktime_to_ms(prevent_sleep_time));
+	seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n",
+		   ws->name, active_count, ws->event_count,
+		   ws->wakeup_count, ws->expire_count,
+		   ktime_to_ms(active_time), ktime_to_ms(total_time),
+		   ktime_to_ms(max_time), ktime_to_ms(ws->last_time),
+		   ktime_to_ms(prevent_sleep_time));
+	ret = m->last_ret;
 
 	spin_unlock_irqrestore(&ws->lock, flags);
 
drivers/mfd/ab8500-debugfs.c +7 -5
index e592bb6..b8a8b16 100644
@@ -1304,8 +1304,9 @@ static int ab8500_registers_print(struct device *dev, u32 bank,
 			}
 
 			if (s) {
-				err = seq_printf(s, "  [0x%02X/0x%02X]: 0x%02X\n",
-					bank, reg, value);
+				seq_printf(s, "  [0x%02X/0x%02X]: 0x%02X\n",
+					   bank, reg, value);
+				err = s->last_ret;
 				if (err < 0) {
 					/* Error is not returned here since
 					 * the output is wanted in any case */
@@ -1355,7 +1356,8 @@ static int ab8500_print_all_banks(struct seq_file *s, void *p)
 	seq_printf(s, AB8500_NAME_STRING " register values:\n");
 
 	for (i = 0; i < AB8500_NUM_BANKS; i++) {
-		err = seq_printf(s, " bank 0x%02X:\n", i);
+		seq_printf(s, " bank 0x%02X:\n", i);
+		err = s->last_ret;
 
 		ab8500_registers_print(dev, i, s);
 	}
@@ -1709,8 +1711,8 @@ static int ab8500_print_modem_registers(struct seq_file *s, void *p)
 			dev_err(dev, "ab->read fail %d\n", err);
 			return err;
 		}
-		err = seq_printf(s, "  [0x%02X/0x%02X]: 0x%02X\n",
-			bank, reg, value);
+		seq_printf(s, "  [0x%02X/0x%02X]: 0x%02X\n",
+			   bank, reg, value);
 	}
 	err = abx500_set_register_interruptible(dev,
 		AB8500_REGU_CTRL1, AB8500_SUPPLY_CONTROL_REG, orig_value);
drivers/mtd/devices/docg3.c +45 -52

138 lines not on this page · raw message

drivers/parisc/ccio-dma.c +26 -26

85 lines not on this page · raw message

drivers/parisc/sba_iommu.c +37 -37

108 lines not on this page · raw message

drivers/regulator/dbx500-prcmu.c +17 -17
index ce89f78..42f7651 100644
@@ -93,15 +93,14 @@ void ux500_regulator_resume_debug(void)
 
 static int ux500_regulator_power_state_cnt_print(struct seq_file *s, void *p)
 {
-	struct device *dev = s->private;
-	int err;
-
 	/* print power state count */
-	err = seq_printf(s, "ux500-regulator power state count: %i\n",
-		power_state_active_get());
-	if (err < 0)
-		dev_err(dev, "seq_printf overflow\n");
+	seq_printf(s, "ux500-regulator power state count: %i\n",
+		   power_state_active_get());
+	if (s->last_ret < 0) {
+		struct device *dev = s->private;
 
+		dev_err(dev, "seq_printf overflow\n");
+	}
 	return 0;
 }
 
@@ -127,13 +126,12 @@ static int ux500_regulator_status_print(struct seq_file *s, void *p)
 	int i;
 
 	/* print dump header */
-	err = seq_printf(s, "ux500-regulator status:\n");
-	if (err < 0)
+	seq_puts(s, "ux500-regulator status:\n");
+	if (s->last_ret < 0)
 		dev_err(dev, "seq_printf overflow\n");
 
-	err = seq_printf(s, "%31s : %8s : %8s\n", "current",
-		"before", "after");
-	if (err < 0)
+	seq_printf(s, "%31s : %8s : %8s\n", "current", "before", "after");
+	if (s->last_ret < 0)
 		dev_err(dev, "seq_printf overflow\n");
 
 	for (i = 0; i < rdebug.num_regulators; i++) {
@@ -142,11 +140,13 @@ static int ux500_regulator_status_print(struct seq_file *s, void *p)
 		info = &rdebug.regulator_array[i];
 
 		/* print status */
-		err = seq_printf(s, "%20s : %8s : %8s : %8s\n", info->desc.name,
-			info->is_enabled ? "enabled" : "disabled",
-			rdebug.state_before_suspend[i] ? "enabled" : "disabled",
-			rdebug.state_after_suspend[i] ? "enabled" : "disabled");
-		if (err < 0)
+		seq_printf(s, "%20s : %8s : %8s : %8s\n",
+			   info->desc.name,
+			   info->is_enabled ? "enabled" : "disabled",
+			   rdebug.state_before_suspend[i] ? "enabled" : "disabled",
+			   rdebug.state_after_suspend[i] ? "enabled" : "disabled");
+		err = s->last_ret;
+		if (s->last_ret < 0)
 			dev_err(dev, "seq_printf overflow\n");
 	}
 
drivers/staging/lustre/lustre/fid/lproc_fid.c +11 -5
index 294070d..503705e 100644
@@ -109,7 +109,9 @@ lprocfs_fid_space_seq_show(struct seq_file *m, void *unused)
 	LASSERT(seq != NULL);
 
 	mutex_lock(&seq->lcs_mutex);
-	rc = seq_printf(m, "["LPX64" - "LPX64"]:%x:%s\n", PRANGE(&seq->lcs_space));
+	seq_printf(m, "[" LPX64 " - " LPX64 "]:%x:%s\n",
+		   PRANGE(&seq->lcs_space));
+	rc = m->last_ret;
 	mutex_unlock(&seq->lcs_mutex);
 
 	return rc;
@@ -158,7 +160,8 @@ lprocfs_fid_width_seq_show(struct seq_file *m, void *unused)
 	LASSERT(seq != NULL);
 
 	mutex_lock(&seq->lcs_mutex);
-	rc = seq_printf(m, LPU64"\n", seq->lcs_width);
+	seq_printf(m, LPU64 "\n", seq->lcs_width);
+	rc = m->last_ret;
 	mutex_unlock(&seq->lcs_mutex);
 
 	return rc;
@@ -173,7 +176,8 @@ lprocfs_fid_fid_seq_show(struct seq_file *m, void *unused)
 	LASSERT(seq != NULL);
 
 	mutex_lock(&seq->lcs_mutex);
-	rc = seq_printf(m, DFID"\n", PFID(&seq->lcs_fid));
+	seq_printf(m, DFID "\n", PFID(&seq->lcs_fid));
+	rc = m->last_ret;
 	mutex_unlock(&seq->lcs_mutex);
 
 	return rc;
@@ -190,9 +194,11 @@ lprocfs_fid_server_seq_show(struct seq_file *m, void *unused)
 
 	if (seq->lcs_exp != NULL) {
 		cli = &seq->lcs_exp->exp_obd->u.cli;
-		rc = seq_printf(m, "%s\n", cli->cl_target_uuid.uuid);
+		seq_printf(m, "%s\n", cli->cl_target_uuid.uuid);
+		rc = m->last_ret;
 	} else {
-		rc = seq_printf(m, "%s\n", seq->lcs_srv->lss_name);
+		seq_printf(m, "%s\n", seq->lcs_srv->lss_name);
+		rc = m->last_ret;
 	}
 	return rc;
 }
drivers/staging/lustre/lustre/llite/lproc_llite.c +21 -11
index 8261d45..8b2a077 100644
@@ -61,8 +61,10 @@ static int ll_blksize_seq_show(struct seq_file *m, void *v)
 	rc = ll_statfs_internal(sb, &osfs,
 				cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
 				OBD_STATFS_NODELAY);
-	if (!rc)
-	      rc = seq_printf(m, "%u\n", osfs.os_bsize);
+	if (!rc) {
+		seq_printf(m, "%u\n", osfs.os_bsize);
+	      rc = m->last_ret;
+}
 
 	return rc;
 }
@@ -85,7 +87,8 @@ static int ll_kbytestotal_seq_show(struct seq_file *m, void *v)
 		while (blk_size >>= 1)
 			result <<= 1;
 
-		rc = seq_printf(m, LPU64"\n", result);
+		seq_printf(m, LPU64 "\n", result);
+		rc = m->last_ret;
 	}
 	return rc;
 }
@@ -108,7 +111,8 @@ static int ll_kbytesfree_seq_show(struct seq_file *m, void *v)
 		while (blk_size >>= 1)
 			result <<= 1;
 
-		rc = seq_printf(m, LPU64"\n", result);
+		seq_printf(m, LPU64 "\n", result);
+		rc = m->last_ret;
 	}
 	return rc;
 }
@@ -131,7 +135,8 @@ static int ll_kbytesavail_seq_show(struct seq_file *m, void *v)
 		while (blk_size >>= 1)
 			result <<= 1;
 
-		rc = seq_printf(m, LPU64"\n", result);
+		seq_printf(m, LPU64 "\n", result);
+		rc = m->last_ret;
 	}
 	return rc;
 }
@@ -147,8 +152,10 @@ static int ll_filestotal_seq_show(struct seq_file *m, void *v)
 	rc = ll_statfs_internal(sb, &osfs,
 				cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
 				OBD_STATFS_NODELAY);
-	if (!rc)
-		 rc = seq_printf(m, LPU64"\n", osfs.os_files);
+	if (!rc) {
+		seq_printf(m, LPU64 "\n", osfs.os_files);
+		 rc = m->last_ret;
+}
 	return rc;
 }
 LPROC_SEQ_FOPS_RO(ll_filestotal);
@@ -163,8 +170,10 @@ static int ll_filesfree_seq_show(struct seq_file *m, void *v)
 	rc = ll_statfs_internal(sb, &osfs,
 				cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
 				OBD_STATFS_NODELAY);
-	if (!rc)
-		 rc = seq_printf(m, LPU64"\n", osfs.os_ffree);
+	if (!rc) {
+		seq_printf(m, LPU64 "\n", osfs.os_ffree);
+		 rc = m->last_ret;
+}
 	return rc;
 }
 LPROC_SEQ_FOPS_RO(ll_filesfree);
@@ -177,9 +186,10 @@ static int ll_client_type_seq_show(struct seq_file *m, void *v)
 	LASSERT(sbi != NULL);
 
 	if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
-		rc = seq_printf(m, "remote client\n");
+		seq_puts(m, "remote client\n");
 	else
-		rc = seq_printf(m, "local client\n");
+		seq_puts(m, "local client\n");
+	rc = m->last_ret;
 
 	return rc;
 }
drivers/staging/lustre/lustre/mdc/lproc_mdc.c +2 -1
index e0b8f18..fc14af2 100644
@@ -48,7 +48,8 @@ static int mdc_max_rpcs_in_flight_seq_show(struct seq_file *m, void *v)
 	int rc;
 
 	client_obd_list_lock(&cli->cl_loi_list_lock);
-	rc = seq_printf(m, "%u\n", cli->cl_max_rpcs_in_flight);
+	seq_printf(m, "%u\n", cli->cl_max_rpcs_in_flight);
+	rc = m->last_ret;
 	client_obd_list_unlock(&cli->cl_loi_list_lock);
 	return rc;
 }
drivers/staging/lustre/lustre/obdclass/lprocfs_status.c +41 -23

141 lines not on this page · raw message

drivers/staging/lustre/lustre/osc/lproc_osc.c +17 -11
index df5ddf8..b42eb5d 100644
@@ -49,7 +49,8 @@ static int osc_active_seq_show(struct seq_file *m, void *v)
 	int rc;
 
 	LPROCFS_CLIMP_CHECK(dev);
-	rc = seq_printf(m, "%d\n", !dev->u.cli.cl_import->imp_deactive);
+	seq_printf(m, "%d\n", !dev->u.cli.cl_import->imp_deactive);
+	rc = m->last_ret;
 	LPROCFS_CLIMP_EXIT(dev);
 	return rc;
 }
@@ -83,7 +84,8 @@ static int osc_max_rpcs_in_flight_seq_show(struct seq_file *m, void *v)
 	int rc;
 
 	client_obd_list_lock(&cli->cl_loi_list_lock);
-	rc = seq_printf(m, "%u\n", cli->cl_max_rpcs_in_flight);
+	seq_printf(m, "%u\n", cli->cl_max_rpcs_in_flight);
+	rc = m->last_ret;
 	client_obd_list_unlock(&cli->cl_loi_list_lock);
 	return rc;
 }
@@ -164,12 +166,13 @@ static int osc_cached_mb_seq_show(struct seq_file *m, void *v)
 	int shift = 20 - PAGE_CACHE_SHIFT;
 	int rc;
 
-	rc = seq_printf(m,
-		      "used_mb: %d\n"
-		      "busy_cnt: %d\n",
-		      (atomic_read(&cli->cl_lru_in_list) +
-			atomic_read(&cli->cl_lru_busy)) >> shift,
-		      atomic_read(&cli->cl_lru_busy));
+	seq_printf(m,
+		   "used_mb: %d\n"
+		   "busy_cnt: %d\n",
+		   (atomic_read(&cli->cl_lru_in_list) +
+		    atomic_read(&cli->cl_lru_busy)) >> shift,
+		   atomic_read(&cli->cl_lru_busy));
+	rc = m->last_ret;
 
 	return rc;
 }
@@ -206,7 +209,8 @@ static int osc_cur_dirty_bytes_seq_show(struct seq_file *m, void *v)
 	int rc;
 
 	client_obd_list_lock(&cli->cl_loi_list_lock);
-	rc = seq_printf(m, "%lu\n", cli->cl_dirty);
+	seq_printf(m, "%lu\n", cli->cl_dirty);
+	rc = m->last_ret;
 	client_obd_list_unlock(&cli->cl_loi_list_lock);
 	return rc;
 }
@@ -219,7 +223,8 @@ static int osc_cur_grant_bytes_seq_show(struct seq_file *m, void *v)
 	int rc;
 
 	client_obd_list_lock(&cli->cl_loi_list_lock);
-	rc = seq_printf(m, "%lu\n", cli->cl_avail_grant);
+	seq_printf(m, "%lu\n", cli->cl_avail_grant);
+	rc = m->last_ret;
 	client_obd_list_unlock(&cli->cl_loi_list_lock);
 	return rc;
 }
@@ -264,7 +269,8 @@ static int osc_cur_lost_grant_bytes_seq_show(struct seq_file *m, void *v)
 	int rc;
 
 	client_obd_list_lock(&cli->cl_loi_list_lock);
-	rc = seq_printf(m, "%lu\n", cli->cl_lost_grant);
+	seq_printf(m, "%lu\n", cli->cl_lost_grant);
+	rc = m->last_ret;
 	client_obd_list_unlock(&cli->cl_loi_list_lock);
 	return rc;
 }
drivers/staging/lustre/lustre/ptlrpc/lproc_ptlrpc.c +2 -1
index 6bda26d..a068df2 100644
@@ -1310,7 +1310,8 @@ int lprocfs_rd_pinger_recov(struct seq_file *m, void *n)
 	int rc;
 
 	LPROCFS_CLIMP_CHECK(obd);
-	rc = seq_printf(m, "%d\n", !imp->imp_no_pinger_recover);
+	seq_printf(m, "%d\n", !imp->imp_no_pinger_recover);
+	rc = m->last_ret;
 	LPROCFS_CLIMP_EXIT(obd);
 
 	return rc;
drivers/staging/lustre/lustre/ptlrpc/sec_bulk.c +39 -39

84 lines not on this page · raw message

drivers/usb/gadget/pxa27x_udc.c +52 -56

156 lines not on this page · raw message

fs/debugfs/file.c +3 -3
index 6314629..2c04599 100644
@@ -699,9 +699,9 @@ int debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
 
 	for (i = 0; i < nregs; i++, regs++) {
 		if (prefix)
-			ret += seq_printf(s, "%s", prefix);
-		ret += seq_printf(s, "%s = 0x%08x\n", regs->name,
-				  readl(base + regs->offset));
+			seq_printf(s, "%s", prefix);
+		seq_printf(s, "%s = 0x%08x\n", regs->name,
+			   readl(base + regs->offset));
 	}
 	return ret;
 }
fs/dlm/debug_fs.c +83 -69

229 lines not on this page · raw message

fs/eventfd.c +3 -2
index 35470d9..08835ba 100644
@@ -293,8 +293,9 @@ static int eventfd_show_fdinfo(struct seq_file *m, struct file *f)
 	int ret;
 
 	spin_lock_irq(&ctx->wqh.lock);
-	ret = seq_printf(m, "eventfd-count: %16llx\n",
-			 (unsigned long long)ctx->count);
+	seq_printf(m, "eventfd-count: %16llx\n",
+		   (unsigned long long)ctx->count);
+	ret = m->last_ret;
 	spin_unlock_irq(&ctx->wqh.lock);
 
 	return ret;
fs/eventpoll.c +4 -3
index 31c729d..35165c5 100644
@@ -844,9 +844,10 @@ static int ep_show_fdinfo(struct seq_file *m, struct file *f)
 	for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
 		struct epitem *epi = rb_entry(rbp, struct epitem, rbn);
 
-		ret = seq_printf(m, "tfd: %8d events: %8x data: %16llx\n",
-				 epi->ffd.fd, epi->event.events,
-				 (long long)epi->event.data);
+		seq_printf(m, "tfd: %8d events: %8x data: %16llx\n",
+			   epi->ffd.fd, epi->event.events,
+			   (long long)epi->event.data);
+		ret = m->last_ret;
 		if (ret)
 			break;
 	}
fs/notify/fdinfo.c +18 -16
index 238a593..05db00a 100644
@@ -58,11 +58,14 @@ static int show_mark_fhandle(struct seq_file *m, struct inode *inode)
 	f.handle.handle_type = ret;
 	f.handle.handle_bytes = size * sizeof(u32);
 
-	ret = seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
-			 f.handle.handle_bytes, f.handle.handle_type);
+	seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
+		   f.handle.handle_bytes, f.handle.handle_type);
+	ret = m->last_ret;
 
-	for (i = 0; i < f.handle.handle_bytes; i++)
-		ret |= seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
+	for (i = 0; i < f.handle.handle_bytes; i++) {
+		seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
+		ret |= m->last_ret;
+	}
 
 	return ret;
 }
@@ -87,11 +90,10 @@ static int inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
 	inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
 	inode = igrab(mark->i.inode);
 	if (inode) {
-		ret = seq_printf(m, "inotify wd:%x ino:%lx sdev:%x "
-				 "mask:%x ignored_mask:%x ",
-				 inode_mark->wd, inode->i_ino,
-				 inode->i_sb->s_dev,
-				 mark->mask, mark->ignored_mask);
+		seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:%x ",
+			   inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
+			   mark->mask, mark->ignored_mask);
+		ret = m->last_ret;
 		ret |= show_mark_fhandle(m, inode);
 		ret |= seq_putc(m, '\n');
 		iput(inode);
@@ -125,19 +127,19 @@ static int fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
 		inode = igrab(mark->i.inode);
 		if (!inode)
 			goto out;
-		ret = seq_printf(m, "fanotify ino:%lx sdev:%x "
-				 "mflags:%x mask:%x ignored_mask:%x ",
-				 inode->i_ino, inode->i_sb->s_dev,
-				 mflags, mark->mask, mark->ignored_mask);
+		seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
+			   inode->i_ino, inode->i_sb->s_dev,
+			   mflags, mark->mask, mark->ignored_mask);
+		ret = m->last_ret;
 		ret |= show_mark_fhandle(m, inode);
 		ret |= seq_putc(m, '\n');
 		iput(inode);
 	} else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) {
 		struct mount *mnt = real_mount(mark->m.mnt);
 
-		ret = seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x "
-				 "ignored_mask:%x\n", mnt->mnt_id, mflags,
-				 mark->mask, mark->ignored_mask);
+		seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
+			   mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);
+		ret = m->last_ret;
 	}
 out:
 	return ret;
fs/seq_file.c +7 -8
index 3135c25..a9585c3 100644
@@ -389,32 +389,31 @@ int seq_escape(struct seq_file *m, const char *s, const char *esc)
 }
 EXPORT_SYMBOL(seq_escape);
 
-int seq_vprintf(struct seq_file *m, const char *f, va_list args)
+void seq_vprintf(struct seq_file *m, const char *f, va_list args)
 {
 	int len;
 
 	if (m->count < m->size) {
 		len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
+		m->last_len = len;
 		if (m->count + len < m->size) {
 			m->count += len;
-			return 0;
+			m->last_ret = 0;
+			return;
 		}
 	}
 	seq_set_overflow(m);
-	return -1;
+	m->last_ret = -1;
 }
 EXPORT_SYMBOL(seq_vprintf);
 
-int seq_printf(struct seq_file *m, const char *f, ...)
+void seq_printf(struct seq_file *m, const char *f, ...)
 {
-	int ret;
 	va_list args;
 
 	va_start(args, f);
-	ret = seq_vprintf(m, f, args);
+	seq_vprintf(m, f, args);
 	va_end(args);
-
-	return ret;
 }
 EXPORT_SYMBOL(seq_printf);
 
include/linux/seq_file.h +4 -2
index 4e32edc..b92994a 100644
@@ -20,6 +20,8 @@ struct seq_file {
 	size_t size;
 	size_t from;
 	size_t count;
+	size_t last_len;
+	int last_ret;
 	loff_t index;
 	loff_t read_pos;
 	u64 version;
@@ -89,8 +91,8 @@ int seq_putc(struct seq_file *m, char c);
 int seq_puts(struct seq_file *m, const char *s);
 int seq_write(struct seq_file *seq, const void *data, size_t len);
 
-__printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);
-__printf(2, 0) int seq_vprintf(struct seq_file *, const char *, va_list args);
+__printf(2, 3) void seq_printf(struct seq_file *, const char *, ...);
+__printf(2, 0) void seq_vprintf(struct seq_file *, const char *, va_list args);
 
 int seq_path(struct seq_file *, const struct path *, const char *);
 int seq_dentry(struct seq_file *, struct dentry *, const char *);
ipc/util.c +4 -2
index e829da9..a4eab9a 100644
@@ -897,8 +897,10 @@ static int sysvipc_proc_show(struct seq_file *s, void *it)
 	struct ipc_proc_iter *iter = s->private;
 	struct ipc_proc_iface *iface = iter->iface;
 
-	if (it == SEQ_START_TOKEN)
-		return seq_puts(s, iface->header);
+	if (it == SEQ_START_TOKEN) {
+		seq_puts(s, iface->header);
+		return s->last_ret;
+}
 
 	return iface->show(s, it);
 }
net/batman-adv/gateway_client.c +11 -10
index 1ce4b87..563b474 100644
@@ -477,16 +477,17 @@ static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
 
 	curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
 
-	ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
-			 (curr_gw == gw_node ? "=>" : "  "),
-			 gw_node->orig_node->orig,
-			 router->tq_avg, router->addr,
-			 router->if_incoming->net_dev->name,
-			 gw_node->orig_node->gw_flags,
-			 (down > 2048 ? down / 1024 : down),
-			 (down > 2048 ? "MBit" : "KBit"),
-			 (up > 2048 ? up / 1024 : up),
-			 (up > 2048 ? "MBit" : "KBit"));
+	seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
+		   (curr_gw == gw_node ? "=>" : "  "),
+		   gw_node->orig_node->orig,
+		   router->tq_avg, router->addr,
+		   router->if_incoming->net_dev->name,
+		   gw_node->orig_node->gw_flags,
+		   (down > 2048 ? down / 1024 : down),
+		   (down > 2048 ? "MBit" : "KBit"),
+		   (up > 2048 ? up / 1024 : up),
+		   (up > 2048 ? "MBit" : "KBit"));
+	ret = seq->last_ret;
 
 	batadv_neigh_node_free_ref(router);
 	if (curr_gw)
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +19 -11
index 4c48e43..247f6c0 100644
@@ -104,7 +104,8 @@ static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
 	if (ret)
 		return 0;
 
-	ret = seq_printf(s, "secctx=%s ", secctx);
+	seq_printf(s, "secctx=%s ", secctx);
+	ret = s->last_ret;
 
 	security_release_secctx(secctx, len);
 	return ret;
@@ -141,10 +142,11 @@ static int ct_seq_show(struct seq_file *s, void *v)
 	NF_CT_ASSERT(l4proto);
 
 	ret = -ENOSPC;
-	if (seq_printf(s, "%-8s %u %ld ",
-		      l4proto->name, nf_ct_protonum(ct),
-		      timer_pending(&ct->timeout)
-		      ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
+	seq_printf(s, "%-8s %u %ld ",
+		   l4proto->name, nf_ct_protonum(ct),
+		   timer_pending(&ct->timeout)
+		   ? (long)(ct->timeout.expires - jiffies)/HZ : 0);
+	if (s->last_ret)
 		goto release;
 
 	if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
@@ -157,9 +159,11 @@ static int ct_seq_show(struct seq_file *s, void *v)
 	if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
 		goto release;
 
-	if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
-		if (seq_printf(s, "[UNREPLIED] "))
+	if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status))) {
+		seq_puts(s, "[UNREPLIED] ");
+		if (s->last_ret)
 			goto release;
+	}
 
 	if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
 			l3proto, l4proto))
@@ -168,19 +172,23 @@ static int ct_seq_show(struct seq_file *s, void *v)
 	if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
 		goto release;
 
-	if (test_bit(IPS_ASSURED_BIT, &ct->status))
-		if (seq_printf(s, "[ASSURED] "))
+	if (test_bit(IPS_ASSURED_BIT, &ct->status)) {
+		seq_puts(s, "[ASSURED] ");
+		if (s->last_ret)
 			goto release;
+	}
 
 #ifdef CONFIG_NF_CONNTRACK_MARK
-	if (seq_printf(s, "mark=%u ", ct->mark))
+	seq_printf(s, "mark=%u ", ct->mark);
+	if (s->last_ret)
 		goto release;
 #endif
 
 	if (ct_show_secctx(s, ct))
 		goto release;
 
-	if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
+	seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
+	if (s->last_ret)
 		goto release;
 	ret = 0;
 release:
net/netfilter/nf_conntrack_standalone.c +22 -13
index 342c216..942d0b0 100644
@@ -129,7 +129,8 @@ static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
 	if (ret)
 		return 0;
 
-	ret = seq_printf(s, "secctx=%s ", secctx);
+	seq_printf(s, "secctx=%s ", secctx);
+	ret = s->last_ret;
 
 	security_release_secctx(secctx, len);
 	return ret;
@@ -193,11 +194,12 @@ static int ct_seq_show(struct seq_file *s, void *v)
 	NF_CT_ASSERT(l4proto);
 
 	ret = -ENOSPC;
-	if (seq_printf(s, "%-8s %u %-8s %u %ld ",
-		       l3proto->name, nf_ct_l3num(ct),
-		       l4proto->name, nf_ct_protonum(ct),
-		       timer_pending(&ct->timeout)
-		       ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
+	seq_printf(s, "%-8s %u %-8s %u %ld ",
+		   l3proto->name, nf_ct_l3num(ct),
+		   l4proto->name, nf_ct_protonum(ct),
+		   timer_pending(&ct->timeout)
+		   ? (long)(ct->timeout.expires - jiffies)/HZ : 0);
+	if (s->last_ret)
 		goto release;
 
 	if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
@@ -210,9 +212,11 @@ static int ct_seq_show(struct seq_file *s, void *v)
 	if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
 		goto release;
 
-	if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
-		if (seq_printf(s, "[UNREPLIED] "))
+	if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status))) {
+		seq_puts(s, "[UNREPLIED] ");
+		if (s->last_ret)
 			goto release;
+	}
 
 	if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
 			l3proto, l4proto))
@@ -221,12 +225,15 @@ static int ct_seq_show(struct seq_file *s, void *v)
 	if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
 		goto release;
 
-	if (test_bit(IPS_ASSURED_BIT, &ct->status))
-		if (seq_printf(s, "[ASSURED] "))
+	if (test_bit(IPS_ASSURED_BIT, &ct->status)) {
+		seq_puts(s, "[ASSURED] ");
+		if (s->last_ret)
 			goto release;
+	}
 
 #if defined(CONFIG_NF_CONNTRACK_MARK)
-	if (seq_printf(s, "mark=%u ", ct->mark))
+	seq_printf(s, "mark=%u ", ct->mark);
+	if (s->last_ret)
 		goto release;
 #endif
 
@@ -234,14 +241,16 @@ static int ct_seq_show(struct seq_file *s, void *v)
 		goto release;
 
 #ifdef CONFIG_NF_CONNTRACK_ZONES
-	if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
+	seq_printf(s, "zone=%u ", nf_ct_zone(ct));
+	if (s->last_ret)
 		goto release;
 #endif
 
 	if (ct_show_delta_time(s, ct))
 		goto release;
 
-	if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
+	seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
+	if (s->last_ret)
 		goto release;
 
 	ret = 0;
net/netfilter/nf_log.c +7 -5
index 5b26144..ba0d1e7 100644
@@ -196,19 +196,21 @@ static int seq_show(struct seq_file *s, void *v)
 					   lockdep_is_held(&nf_log_mutex));
 
 	if (!logger)
-		ret = seq_printf(s, "%2lld NONE (", *pos);
+		seq_printf(s, "%2lld NONE (", *pos);
 	else
-		ret = seq_printf(s, "%2lld %s (", *pos, logger->name);
-
+		seq_printf(s, "%2lld %s (", *pos, logger->name);
+	ret = s->last_ret;
 	if (ret < 0)
 		return ret;
 
 	list_for_each_entry(t, &nf_loggers_l[*pos], list[*pos]) {
-		ret = seq_printf(s, "%s", t->name);
+		seq_printf(s, "%s", t->name);
+		ret = s->last_ret;
 		if (ret < 0)
 			return ret;
 		if (&t->list[*pos] != nf_loggers_l[*pos].prev) {
-			ret = seq_printf(s, ",");
+			seq_puts(s, ",");
+			ret = s->last_ret;
 			if (ret < 0)
 				return ret;
 		}
net/netfilter/xt_hashlimit.c +18 -16
index 9ff035c..e4b4da0 100644
@@ -794,25 +794,27 @@ static int dl_seq_real_show(struct dsthash_ent *ent, u_int8_t family,
 
 	switch (family) {
 	case NFPROTO_IPV4:
-		res = seq_printf(s, "%ld %pI4:%u->%pI4:%u %u %u %u\n",
-				 (long)(ent->expires - jiffies)/HZ,
-				 &ent->dst.ip.src,
-				 ntohs(ent->dst.src_port),
-				 &ent->dst.ip.dst,
-				 ntohs(ent->dst.dst_port),
-				 ent->rateinfo.credit, ent->rateinfo.credit_cap,
-				 ent->rateinfo.cost);
+		seq_printf(s, "%ld %pI4:%u->%pI4:%u %u %u %u\n",
+			   (long)(ent->expires - jiffies)/HZ,
+			   &ent->dst.ip.src,
+			   ntohs(ent->dst.src_port),
+			   &ent->dst.ip.dst,
+			   ntohs(ent->dst.dst_port),
+			   ent->rateinfo.credit, ent->rateinfo.credit_cap,
+			   ent->rateinfo.cost);
+		res = s->last_ret;
 		break;
 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
 	case NFPROTO_IPV6:
-		res = seq_printf(s, "%ld %pI6:%u->%pI6:%u %u %u %u\n",
-				 (long)(ent->expires - jiffies)/HZ,
-				 &ent->dst.ip6.src,
-				 ntohs(ent->dst.src_port),
-				 &ent->dst.ip6.dst,
-				 ntohs(ent->dst.dst_port),
-				 ent->rateinfo.credit, ent->rateinfo.credit_cap,
-				 ent->rateinfo.cost);
+		seq_printf(s, "%ld %pI6:%u->%pI6:%u %u %u %u\n",
+			   (long)(ent->expires - jiffies)/HZ,
+			   &ent->dst.ip6.src,
+			   ntohs(ent->dst.src_port),
+			   &ent->dst.ip6.dst,
+			   ntohs(ent->dst.dst_port),
+			   ent->rateinfo.credit, ent->rateinfo.credit_cap,
+			   ent->rateinfo.cost);
+		res = s->last_ret;
 		break;
 #endif
 	default:
author
#8 lore raw
Quoted 11 lines · Joe Perches
Quoted 4 lines · George Spelvin
Quoted 1 line

Maybe WARN_ONCE so it's easier to emit the format too.

Good idea. And, if it's not too much trouble, a comment explaining why it's deliberately omitted so the issue doesn't arise again.

Before any of the %n uses could be removed, I believe seq_printf could to be converted to return void and have a another mechanism to determine if any error occurred and the length of the output of seq_printf.

Quoted 6 lines

I've done a preliminary conversion of seq_printf and seq_vprintf to return void and added last_ret and last_len to struct seq_file.

If that's applied, it's trivial to convert vsnprintf to skip %n.

Anyone have an opinion of a different conversion mechanism?

Maybe I missed this somewhere in the thread, but I'm not sure I understand the move to "void". Here's what I see, please correct me:

1- seq_printf currently returns success/failure
2- some callers of seq_printf (correctly) use the return value as success/failure indication
3- some callers of seq_printf (incorrectly) use the return value as a length indication
4- both success/failure and length are important outputs from seq_printf 5- we need a way to access the length written during the call 6- want to minimize impact on the code base

Due to 1 and 2, it seems like there's no sense in changing the return value to void. Success/failure is already returned, and there are users of it. No sense changing them.

The normal way to handle multiple return values (4 and 5) is to add a pointer argument. For example: seq_printf(s, &len, fmt, args...) where len can be NULL. But this runs against 6.

Due to 6, to solve 4 and 5, usually macro or inline tricks are used, for example:

__printf(3, 4) int seq_printf_len(struct seq_file *, size_t *len, ...); #define seq_printf(s, fmt, ...) seq_printf_len(s, NULL, fmt, ##__VA_ARGS__)

With this, solving 3 becomes possible (your void patch has already detected all the users of the return value, so we can sort out which expect length and which expect success/failure), and lets us actually remove the %n uses trivially too.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#9 lore raw
Quoted 15 lines · Kees Cook
Quoted 14 lines · Joe Perches
Quoted 4 lines · George Spelvin
Quoted 1 line

Maybe WARN_ONCE so it's easier to emit the format too.

Good idea. And, if it's not too much trouble, a comment explaining why it's deliberately omitted so the issue doesn't arise again.

Before any of the %n uses could be removed, I believe seq_printf could to be converted to return void and have a another mechanism to determine if any error occurred and the length of the output of seq_printf.

I've done a preliminary conversion of seq_printf and seq_vprintf to return void and added last_ret and last_len to struct seq_file.

Quoted 6 lines
Quoted 3 lines

If that's applied, it's trivial to convert vsnprintf to skip %n.

Anyone have an opinion of a different conversion mechanism?

Maybe I missed this somewhere in the thread, but I'm not sure I understand the move to "void".

Hi Kees.

Al Viro suggested just fixing the misuses.
https://lkml.org/lkml/2013/9/11/801

David Laight suggested converting to void.
https://lkml.org/lkml/2013/9/12/113

Quoted 9 lines

Here's what I see, please correct me:

1- seq_printf currently returns success/failure
2- some callers of seq_printf (correctly) use the return value as success/failure indication
3- some callers of seq_printf (incorrectly) use the return value as a length indication
4- both success/failure and length are important outputs from seq_printf 5- we need a way to access the length written during the call

Right and I agree with all of that.

Quoted 1 line

6- want to minimize impact on the code base

Maybe not.

Quoted 3 lines

Due to 1 and 2, it seems like there's no sense in changing the return value to void. Success/failure is already returned, and there are users of it. No sense changing them.

Except that future code might expect len instead of bool. I did and would again.

So, I really don't care much and I don't really want to paper over existing misuses, so I choose to fix them all.

Quoted 8 lines

The normal way to handle multiple return values (4 and 5) is to add a pointer argument. For example: seq_printf(s, &len, fmt, args...) where len can be NULL. But this runs against 6.

Due to 6, to solve 4 and 5, usually macro or inline tricks are used, for example:
__printf(3, 4) int seq_printf_len(struct seq_file *, size_t *len, ...); #define seq_printf(s, fmt, ...) seq_printf_len(s, NULL, fmt, ##__VA_ARGS__)

I'd rather fix the code that's defective than paper over the defects with macro tricks.

Quoted 3 lines

With this, solving 3 becomes possible (your void patch has already detected all the users of the return value, so we can sort out which expect length and which expect success/failure),

I (believe I) did that.

Quoted 2 lines

and lets us actually
remove the %n uses trivially too.

Soon I hope.

Anyone else have an opinion?

#10 lore raw
Quoted 1 line

Anyone else have an opinion?

tl;dr: seq_printf() whould return void.

Well, certainly *if* seq_printf returns a value, it should be consistent with printf, i.e. length or -errno.

If it's going to be anything else, then it should be incompatible with an integer, so attempted uses cause compile-time errors. Except for the fairly unreasonable options of a pointer or a structure, void is the only available type.

Looking at the callers, a lot of them are trying to compute a summary length, which is actually really easy to find by just snapshotting m->count before and after the region being printed. And there's an existing seq_overflow() function for detecting errors.

But more importantly, seq_show doesn't *need* the total length returned! What they various show() functions are achieving by summing the return values from seq_printf is generating 0 if all prints succeeded and some negative number of failed prints otherwise.

But show() is supposed to return -errno, and *not* return an error on overflow (fs/seq_file.c/traverse() checks seq_overflow separately), so this is Just Plain Broken. This pattern appears everywhere, and it appears that somebody misunderstood the specs once, and the result has been cargo cult copied all over the kernel.

What happens on overflow is that you get some random errno returned to user space. Bad bad bad.

Since show() functions *aren't supposed to check* for overflows from seq_printf (if they do, it breaks the "reallocate and try again" logic), I support making it return void so that this broken code style errors out and someone has to really try to mess it up.

(Another code stupidity is that I have no idea why traverse() doesn't just take care of the -EAGAIN case internally and simplify the calling convention; it's not like either of the callers check any conditions before calling right back in again.)

maintainer
#11 lore raw
In reply to Kees Cook
Quoted 6 lines · Kees Cook

Maybe I missed this somewhere in the thread, but I'm not sure I understand the move to "void". Here's what I see, please correct me:

1- seq_printf currently returns success/failure
2- some callers of seq_printf (correctly) use the return value as success/failure indication

Not all uses as success/failure are right, at that - you should *NOT* return non-zero from ->show() on overflow. Ever. It should only happen when you have a hard error and do *not* want the operation retried. On success ->show() returns zero - not the number of characters written or anything like that.

Quoted 4 lines

3- some callers of seq_printf (incorrectly) use the return value as a length indication

4- both success/failure and length are important outputs from seq_printf

Not really. Success/failure is used if you want to optimize ->show() a bit; usually you don't. Again, failure == overflow and it's both rare *and* will cause all subsequent seq_...() to be no-ops until the retry. Which retry will follow. Shortly.

Quoted 1 line

5- we need a way to access the length written during the call

_Very_ rarely. And I'd seriously suggest the use of %n in such cases.

Quoted 1 line

6- want to minimize impact on the code base

Majority of the code base simply calls seq_printf() without caring what it returns. Very small minority is broken and needs to be fixed.

Quoted 3 lines

Due to 1 and 2, it seems like there's no sense in changing the return value to void. Success/failure is already returned, and there are users of it. No sense changing them.

... most of them incorrect.

Quoted 7 lines

The normal way to handle multiple return values (4 and 5) is to add a pointer argument. For example: seq_printf(s, &len, fmt, args...) where len can be NULL. But this runs against 6.

Due to 6, to solve 4 and 5, usually macro or inline tricks are used, for example:

Quoted 7 lines

__printf(3, 4) int seq_printf_len(struct seq_file *, size_t *len, ...); #define seq_printf(s, fmt, ...) seq_printf_len(s, NULL, fmt, ##__VA_ARGS__)

With this, solving 3 becomes possible (your void patch has already detected all the users of the return value, so we can sort out which expect length and which expect success/failure), and lets us actually remove the %n uses trivially too.

Consider that NAKed. Reason: fucking ugly and pointless at the same time.

I don't believe that seq_printf() itself needs to be changed at all (or that %n should be removed, actually). Callers should be audited and fixed, of course. With dire warnings added to seq_file.[ch]. And no, it shouldn't be returning void - the current calling conventions are actually right.

#12 lore raw
In reply to Kees Cook
Quoted 2 lines · Kees Cook

3- some callers of seq_printf (incorrectly) use the return value as a length indication

Are there really?

Is somebody using the return value from seq_printf() like

  pos = snprintf(buf, sizeof(buf) - 1, "%s", foo);
  snprintf(buf + pos, sizeof(buf) - 1 - pos, "%s", bar);

? Since the caller cannot pass the return value from seq_printf() like

  pos = seq_printf(m, "%s", foo);
  seq_printf(m + pos, "%s", bar);

, I wonder who would interpret the return value as a length indication.

Even bad code which has never tested failure case, the authors should already know that "seq_printf() returns 0 on success case".

I think that

	pos += seq_printf(m, "%s", foo);
	pos += seq_printf(m, "%s", bar);

is used as the equivalent to

	if (seq_printf(m, "%s", foo))
		pos = -1;
	if (seq_printf(m, "%s", bar))
		pos = -1;

.

arch/arm/plat-pxa/dma.c static int dbg_show_state(struct seq_file *s, void *p) · line 178

Quoted 11 lines · Joe Perches
arch/arm/plat-pxa/dma.c +2 -2
@@ -174,8 +171,8 @@ static int dbg_show_state(struct seq_file *s, void *p)
 	int pos = 0;
 
 	/* basic device status */
-	pos += seq_printf(s, "DMA engine status\n");
-	pos += seq_printf(s, "\tChannel number: %d\n", num_dma_channels);
+	seq_puts(s, "DMA engine status\n");
+	seq_printf(s, "\tChannel number: %d\n", num_dma_channels);
 
 	return pos;
 }

As I described above, I think this change breaks the functionality. We need to change like

  -	pos += seq_printf(s, "DMA engine status\n");
  -	pos += seq_printf(s, "\tChannel number: %d\n", num_dma_channels);
  +	pos |= seq_puts(s, "DMA engine status\n");
  +	pos |= seq_printf(s, "\tChannel number: %d\n", num_dma_channels);

or

  -	pos += seq_printf(s, "DMA engine status\n");
  -	pos += seq_printf(s, "\tChannel number: %d\n", num_dma_channels);
  +	seq_puts(s, "DMA engine status\n");
  +	seq_printf(s, "\tChannel number: %d\n", num_dma_channels);

- return pos;
+ return seq_overflow(s) : -1 : 0;

for keeping the functionality.

maintainer
#13 lore raw
Quoted 2 lines · Tetsuo Handa

Even bad code which has never tested failure case, the authors should already know that "seq_printf() returns 0 on success case".

It is designed so that not testing failure case is normal approach for the majority of users.

Quoted 8 lines
  -	pos += seq_printf(s, "\tChannel number: %d\n", num_dma_channels);
  +	seq_puts(s, "DMA engine status\n");
  +	seq_printf(s, "\tChannel number: %d\n", num_dma_channels);

- return pos;
+ return seq_overflow(s) : -1 : 0;

for keeping the functionality.

ITYM "for keeping the bug". Read seq_read(), please. Any negative value returned by ->show() is a hard error. It won't be retried with bigger buffer; read(2) will *fail*. With -EINVAL, in your case.

We really, really should not return non-zero on overflow. Moreover, returning a _positive_ value (SEQ_SKIP, normally, but any positive will do the same thing) means "silently discard everything ->show() might have produced"

Again, the normal return value of ->show() is 0 and that includes the case of overflow. THE ONLY reason to check for overflow early is when subsequent output of ->show() takes long to generate and we want to skip that and have seq_read() do realloc-and-call-show-again immediately. And in that case the right fix is often to get saner iterator and stop shoving everything into a single ->show() call...

maintainer
#14 lore raw
Quoted 6 lines · Al Viro

Again, the normal return value of ->show() is 0 and that includes the case of overflow. THE ONLY reason to check for overflow early is when subsequent output of ->show() takes long to generate and we want to skip that and have seq_read() do realloc-and-call-show-again immediately. And in that case the right fix is often to get saner iterator and stop shoving everything into a single ->show() call...

Actually, let's take a look at the suckers.

* a bunch in arch/arm/plat-pxa/dma.c:dbg_show_requester_chan(): all buggy;
shouldn't care about pos at all and return 0. Additionally,
dbg_show_descriptors() is buggy _and_ dumb - max_show shouldn't exist at all and it shouldn't be using single_open(); it's iterating over a linked list, for fsck sake...

* cris fasttimer: apparently valid "skip pointless work if we'd overflown" uses; not sure if it's really needed here.

* cris show_cpuinfo(): buggy, should return 0 regardless.

* microblaze show_cpuinfo(): pointless sum is calculated and never used.

* openrisc show_cpuinfo(): buggy, should return 0 regardless.

* s390 pci_perf_show(): ditto.

* mtrr_seq_show(): pointless sum is calculated and never used.

* print_wakeup_source_stats(): buggy, should return 0 regardless.

* i8k_proc_show(): ditto.

* 3 ->show() instances in impi: ditto.

* i2o_report_query_status(): ditto.

* drivers/mfd/ab8500-debugfs.c:ab8500_registers_print(): legimitate use, a side effect of lousy iterator, unfortunately complicated by the same thing used for printk as well.

* same file, in ab8500_print_modem_registers() - return value of seq_printf() is stored in local variable and immediately overwritten.

* same file, a metric arseload of return seq_printf(....): buggy, should return 0.

* doc3g: ditto.

* drivers/parisc callers: pointless sums calculated and never used.

* drivers/regulator/dbx500-prcmu.c: somebody got religious about reporting !!!scary!!! overflows. Even though debugging printks are completely pointless there.

* drivers/rtc: return value passed to caller and discarded there. If it ever stops being discarded, we'll get a bug.

drivers/s390/cio/blacklist.c: buggy, should return 0.

lustre: a bunch "should return 0" cases, AFAICS all of them are of that kind.

rtl8192*: a couple of such cases.

* drivers/usb/gadget/goku_udc.c: apparently valid uses, this time with what looks like a damn good reason to skip extra work - it's reading from IO ports and that can be slow.

* drivers/usb/gadget/pxa27x_udc.c: pointless sums discarded.

* debugfs_print_regs32(): should return 0. The only caller ignores its return value, fortunately, otherwise we'd have bugs.

* fs/dlm: valid, but very likely to be begging for better iterators.

* fs/proc children_seq_show(): buggy, should return 0.

* sysvipc_proc_show() and stuff it's calling (i.e. all show_... in ipc/*): buggy, should return 0.

Enough for now...

Overall: I suspect that Joe might be right. The very few callers that use the return value and use it correctly can bloody well call seq_overflow(), preferably with a detailed comment about the reasons for doing so. Anything that really wants the length of output (if we have such places at all) can use %n or see Figure 1. I haven't crawled through lib/*, net/* and sound/* yet, but that's how the things look so far.

maintainer
#15 lore raw
Quoted 7 lines · Al Viro

Overall: I suspect that Joe might be right. The very few callers that use the return value and use it correctly can bloody well call seq_overflow(), preferably with a detailed comment about the reasons for doing so. Anything that really wants the length of output (if we have such places at all) can use %n or see Figure 1. I haven't crawled through lib/*, net/* and sound/* yet, but that's how the things look so far.

The same goes for seq_puts, seq_escape, seq_vprintf, seq_dentry, seq_bitmap*, seq_cpumask*, seq_nodemask*, seq_putc, seq_put_decimal*

seq_puts() has one buggy user trying to return its return value from ->show(). seq_putc() has several such.

seq_path() returns length and in one case its return value is used (right-padded pathname in /proc/swaps).

seq_path_root() returns what would be a valid return value for ->show() (0 or 1, actually).

seq_write() return value is mostly ignored; kernel/trace/* is using it to check for overflows, but its reaction to said overflows is odd.

The bottom line: most of these guys could as well return void; we have few overflow checks and those could be made explicit. As it is, "return -1 on overflow" had been a mistake. Mea culpa.

#16 lore raw
Quoted 3 lines · Al Viro

The bottom line: most of these guys could as well return void; we have few overflow checks and those could be made explicit. As it is, "return -1 on overflow" had been a mistake.

What do you think of adding last_ret and last_len to struct seq_file? Is there any case where it's racy?

I haven't noticed one, but dunno.

Another option might be to use something like:

struct seq_rtn {
	int rtn;
	size_t len;
}

as the return for all the seq_<foo> funcs.

#17 lore raw
In reply to Kees Cook
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Kees Cook
Quoted 4 lines · Joe Perches
Quoted 1 line · Kees Cook

The %n format is not ignored, so remove the incorrect comment about it.

I think it may be better to reimplement the ignoring.

Yeah, just had a quick look, and scanf doesn't use this code at all. I'd much rather remove %n again instead.

Why would you want to artificially make the function diverge from the spec? People shouldn't be caught by surprises if at all possible, and one can certainly not expect people to go look at the comment before the function implementation to find out what basic (standard) features _do not_ work (one can expect so when trying to find out about _extensions_).

Jan

author
#18 lore raw
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Kees Cook
Quoted 4 lines · Joe Perches
Quoted 1 line · Kees Cook

The %n format is not ignored, so remove the incorrect comment about it.

I think it may be better to reimplement the ignoring.

Yeah, just had a quick look, and scanf doesn't use this code at all. I'd much rather remove %n again instead.

Quoted 6 lines

Why would you want to artificially make the function diverge from the spec? People shouldn't be caught by surprises if at all possible, and one can certainly not expect people to go look at the comment before the function implementation to find out what basic (standard) features _do not_ work (one can expect so when trying to find out about _extensions_).

The spec, in this case, is considered harmful. Without %n, format string flaws are at worst "just" information leaks. Being able to downgrade potential arbitrary memory writing vulnerabilities into info leak vulnerabilities would be a very nice improvement. As another point of reference, even Android's libc replacement, bionic, doesn't implement %n for the very same reasons.

Since there are so few users of %n in the kernel, it seems that this goal is not totally outside the realm of possibility in the short-term. The inclusion of the WARN_ON() was suggested for the very reason of helping a potential user of %n to notice that it's not supported.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#19 lore raw
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Kees Cook
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Kees Cook
Quoted 4 lines · Joe Perches
Quoted 1 line · Kees Cook

The %n format is not ignored, so remove the incorrect comment about it.

I think it may be better to reimplement the ignoring.

Yeah, just had a quick look, and scanf doesn't use this code at all. I'd much rather remove %n again instead.

Quoted 6 lines

Why would you want to artificially make the function diverge from the spec? People shouldn't be caught by surprises if at all possible, and one can certainly not expect people to go look at the comment before the function implementation to find out what basic (standard) features _do not_ work (one can expect so when trying to find out about _extensions_).

Quoted 6 lines

The spec, in this case, is considered harmful. Without %n, format string flaws are at worst "just" information leaks. Being able to downgrade potential arbitrary memory writing vulnerabilities into info leak vulnerabilities would be a very nice improvement. As another point of reference, even Android's libc replacement, bionic, doesn't implement %n for the very same reasons.

Pretty strange argumentation: You disallow people knowing what they do using this just to keep people not knowing what they do from making mistakes? That may make sense in script languages, but not in C (not to speak of in the kernel). In the end this seems like enforcing a Google policy on everyone. But sure - if Linus is willing to merge this, my objection likely doesn't count much.

Jan

#20 lore raw
In reply to Jan Beulich
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Kees Cook
Quoted 4 lines · Joe Perches
Quoted 1 line · Kees Cook

The %n format is not ignored, so remove the incorrect comment about it.

I think it may be better to reimplement the ignoring.

Yeah, just had a quick look, and scanf doesn't use this code at all. I'd much rather remove %n again instead.

Quoted 8 lines

Why would you want to artificially make the function diverge from the spec? People shouldn't be caught by surprises if at all possible, and one can certainly not expect people to go look at the comment before the function implementation to find out what basic (standard) features _do not_ work (one can expect so when trying to find out about _extensions_).

Jan

Actually it's the reverse. I was expecting that %n would be ignored from the start. Then I looked at the file and the comment said that %n was ignored. It's only Kees who looked at the actual code and saw that it wasn't being ignored since 2009.

Kees has been fixing format strings bugs in the past few months and there are probably other out of tree drivers where this bug is still exploitable. It's quite serious.

regards,
dan carpenter

#21 lore raw
In reply to Jan Beulich
Quoted 2 lines

Why would you want to artificially make the function diverge from the spec?

Because %n make it easy to convert a not-uncommon format string bug into a code injection. Thus, poses a significant security vulnerability.

Since it's an obscure and rarely-used feature, it is straightforward to eliminate all users in the Linux kernel, making removing it possible.

I agree that if it were harmless, it would be useful to leave it implemented just for simplicity (it's a trivial amount of code), but it's not harmless.

Quoted 5 lines

People shouldn't be caught by surprises if at all possible, and one can certainly not expect people to go look at the comment before the function implementation to find out what basic (standard) features _do not_ work (one can expect so when trying to find out about _extensions_).

This is why people propose implementing it as a kernel warning.

Strongly support this change.