git: 60e749da3c8b - main - mbuf_tags: use explicitly sized type for 'type' parameter

From: Roger Pau Monné <royger_at_FreeBSD.org>
Date: Wed, 29 Dec 2021 08:25:25 UTC
The branch main has been updated by royger:

URL: https://cgit.FreeBSD.org/src/commit/?id=60e749da3c8b9b42cb7b044bea0b380daf1e70e9

commit 60e749da3c8b9b42cb7b044bea0b380daf1e70e9
Author:     Roger Pau Monné <royger@FreeBSD.org>
AuthorDate: 2021-12-28 08:13:57 +0000
Commit:     Roger Pau Monné <royger@FreeBSD.org>
CommitDate: 2021-12-29 08:23:52 +0000

    mbuf_tags: use explicitly sized type for 'type' parameter
    
    Functions manipulating mbuf tags are using an int type for passing the
    'type' parameter, but the internal tag storage is using a 16bit
    integer to store it. This leads to the following code:
    
    t = m_tag_alloc(...,0xffffffff,...,...);
    m_tag_prepend(m, t);
    r = m_tag_locate(m ,...,0xffffffff, NULL);
    
    Returning r == NULL because m_tag_locate doesn't truncate the type
    parameter when doing the match. This is unexpected because the type of
    the 'type' parameter is int, and the caller doesn't need to know about
    the internal truncations.
    
    Fix this by making the 'type' parameter of type uint16_t in order to
    match the size of its internal storage and make it obvious to the
    caller the actual size of the parameter.
    
    While there also use uint uniformly replacing the existing u_int
    instances.
    
    Reviewed by: kp, donner, glebius
    Differential revision: https://reviews.freebsd.org/D33680
---
 share/man/man9/mbuf_tags.9 | 10 +++++-----
 sys/kern/uipc_mbuf2.c      |  4 ++--
 sys/sys/mbuf.h             | 11 ++++++-----
 3 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/share/man/man9/mbuf_tags.9 b/share/man/man9/mbuf_tags.9
index 66be8377b100..9f3e8c064054 100644
--- a/share/man/man9/mbuf_tags.9
+++ b/share/man/man9/mbuf_tags.9
@@ -20,7 +20,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd January 12, 2008
+.Dd December 28, 2021
 .Dt MBUF_TAGS 9
 .Os
 .Sh NAME
@@ -29,7 +29,7 @@
 .Sh SYNOPSIS
 .In sys/mbuf.h
 .Ft "struct m_tag *"
-.Fn m_tag_alloc "uint32_t cookie" "int type" "int len" "int wait"
+.Fn m_tag_alloc "uint32_t cookie" "uint16_t type" "int len" "int wait"
 .Ft "struct m_tag *"
 .Fn m_tag_copy "struct m_tag *t" "int how"
 .Ft int
@@ -41,17 +41,17 @@
 .Ft void
 .Fn m_tag_delete_nonpersistent "struct mbuf *m"
 .Ft "struct m_tag *"
-.Fn m_tag_find "struct mbuf *m" "int type" "struct m_tag *start"
+.Fn m_tag_find "struct mbuf *m" "uint16_t type" "struct m_tag *start"
 .Ft "struct m_tag *"
 .Fn m_tag_first "struct mbuf *m"
 .Ft void
 .Fn m_tag_free "struct m_tag *t"
 .Ft "struct m_tag *"
-.Fn m_tag_get "int type" "int len" "int wait"
+.Fn m_tag_get "uint16_t type" "int len" "int wait"
 .Ft void
 .Fn m_tag_init "struct mbuf *m"
 .Ft struct m_tag *
-.Fn m_tag_locate "struct mbuf *m" "uint32_t cookie" "int type" "struct m_tag *t"
+.Fn m_tag_locate "struct mbuf *m" "uint32_t cookie" "uint16_t type" "struct m_tag *t"
 .Ft "struct m_tag *"
 .Fn m_tag_next "struct mbuf *m" "struct m_tag *t"
 .Ft void
diff --git a/sys/kern/uipc_mbuf2.c b/sys/kern/uipc_mbuf2.c
index 40aa45d3cfb4..571751e7fd51 100644
--- a/sys/kern/uipc_mbuf2.c
+++ b/sys/kern/uipc_mbuf2.c
@@ -314,7 +314,7 @@ m_tag_free_default(struct m_tag *t)
 
 /* Get a packet tag structure along with specified data following. */
 struct m_tag *
-m_tag_alloc(uint32_t cookie, int type, int len, int wait)
+m_tag_alloc(uint32_t cookie, uint16_t type, int len, int wait)
 {
 	struct m_tag *t;
 
@@ -376,7 +376,7 @@ m_tag_delete_nonpersistent(struct mbuf *m)
 
 /* Find a tag, starting from a given position. */
 struct m_tag *
-m_tag_locate(struct mbuf *m, uint32_t cookie, int type, struct m_tag *t)
+m_tag_locate(struct mbuf *m, uint32_t cookie, uint16_t type, struct m_tag *t)
 {
 	struct m_tag *p;
 
diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h
index 07a75bd5b47b..cea2c3820143 100644
--- a/sys/sys/mbuf.h
+++ b/sys/sys/mbuf.h
@@ -1369,11 +1369,12 @@ extern bool		mb_use_ext_pgs;	/* Use ext_pgs for sendfile */
 /* Specific cookies and tags. */
 
 /* Packet tag routines. */
-struct m_tag	*m_tag_alloc(u_int32_t, int, int, int);
+struct m_tag	*m_tag_alloc(uint32_t, uint16_t, int, int);
 void		 m_tag_delete(struct mbuf *, struct m_tag *);
 void		 m_tag_delete_chain(struct mbuf *, struct m_tag *);
 void		 m_tag_free_default(struct m_tag *);
-struct m_tag	*m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *);
+struct m_tag	*m_tag_locate(struct mbuf *, uint32_t, uint16_t,
+    struct m_tag *);
 struct m_tag	*m_tag_copy(struct m_tag *, int);
 int		 m_tag_copy_chain(struct mbuf *, const struct mbuf *, int);
 void		 m_tag_delete_nonpersistent(struct mbuf *);
@@ -1395,7 +1396,7 @@ m_tag_init(struct mbuf *m)
  * XXX probably should be called m_tag_init, but that was already taken.
  */
 static __inline void
-m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len)
+m_tag_setup(struct m_tag *t, uint32_t cookie, uint16_t type, int len)
 {
 
 	t->m_tag_id = type;
@@ -1457,13 +1458,13 @@ m_tag_unlink(struct mbuf *m, struct m_tag *t)
 #define	MTAG_ABI_COMPAT		0		/* compatibility ABI */
 
 static __inline struct m_tag *
-m_tag_get(int type, int length, int wait)
+m_tag_get(uint16_t type, int length, int wait)
 {
 	return (m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait));
 }
 
 static __inline struct m_tag *
-m_tag_find(struct mbuf *m, int type, struct m_tag *start)
+m_tag_find(struct mbuf *m, uint16_t type, struct m_tag *start)
 {
 	return (SLIST_EMPTY(&m->m_pkthdr.tags) ? (struct m_tag *)NULL :
 	    m_tag_locate(m, MTAG_ABI_COMPAT, type, start));