Commit beba006074e7170d3bc91470c8a6c914730d4c63

Authored by Artem Bityutskiy
1 parent d34315da91

UBIFS: use snprintf instead of sprintf when printing keys

Switch to 'snprintf()' which is more secure and reliable. This is also a
preparation to the subsequent key printing fixes.

Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>

Showing 1 changed file with 20 additions and 18 deletions Side-by-side Diff

... ... @@ -103,8 +103,8 @@
103 103 }
104 104 }
105 105  
106   -static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
107   - char *buffer)
  106 +static void snprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
  107 + char *buffer, int len)
108 108 {
109 109 char *p = buffer;
110 110 int type = key_type(c, key);
111 111  
112 112  
113 113  
114 114  
115 115  
116 116  
117 117  
... ... @@ -112,44 +112,46 @@
112 112 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
113 113 switch (type) {
114 114 case UBIFS_INO_KEY:
115   - sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
116   - get_key_type(type));
  115 + len -= snprintf(p, len, "(%lu, %s)",
  116 + (unsigned long)key_inum(c, key),
  117 + get_key_type(type));
117 118 break;
118 119 case UBIFS_DENT_KEY:
119 120 case UBIFS_XENT_KEY:
120   - sprintf(p, "(%lu, %s, %#08x)",
121   - (unsigned long)key_inum(c, key),
122   - get_key_type(type), key_hash(c, key));
  121 + len -= snprintf(p, len, "(%lu, %s, %#08x)",
  122 + (unsigned long)key_inum(c, key),
  123 + get_key_type(type), key_hash(c, key));
123 124 break;
124 125 case UBIFS_DATA_KEY:
125   - sprintf(p, "(%lu, %s, %u)",
126   - (unsigned long)key_inum(c, key),
127   - get_key_type(type), key_block(c, key));
  126 + len -= snprintf(p, len, "(%lu, %s, %u)",
  127 + (unsigned long)key_inum(c, key),
  128 + get_key_type(type), key_block(c, key));
128 129 break;
129 130 case UBIFS_TRUN_KEY:
130   - sprintf(p, "(%lu, %s)",
131   - (unsigned long)key_inum(c, key),
132   - get_key_type(type));
  131 + len -= snprintf(p, len, "(%lu, %s)",
  132 + (unsigned long)key_inum(c, key),
  133 + get_key_type(type));
133 134 break;
134 135 default:
135   - sprintf(p, "(bad key type: %#08x, %#08x)",
136   - key->u32[0], key->u32[1]);
  136 + len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
  137 + key->u32[0], key->u32[1]);
137 138 }
138 139 } else
139   - sprintf(p, "bad key format %d", c->key_fmt);
  140 + len -= snprintf(p, len, "bad key format %d", c->key_fmt);
  141 + ubifs_assert(len > 0);
140 142 }
141 143  
142 144 const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
143 145 {
144 146 /* dbg_lock must be held */
145   - sprintf_key(c, key, dbg_key_buf0);
  147 + snprintf_key(c, key, dbg_key_buf0, sizeof(dbg_key_buf0) - 1);
146 148 return dbg_key_buf0;
147 149 }
148 150  
149 151 const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
150 152 {
151 153 /* dbg_lock must be held */
152   - sprintf_key(c, key, dbg_key_buf1);
  154 + snprintf_key(c, key, dbg_key_buf1, sizeof(dbg_key_buf1) - 1);
153 155 return dbg_key_buf1;
154 156 }
155 157