@@ -90,7 +90,7 @@ static struct ima_template_field *lookup_template_field(const char *field_id)
90
90
return NULL ;
91
91
}
92
92
93
- static int template_fmt_size (char * template_fmt )
93
+ static int template_fmt_size (const char * template_fmt )
94
94
{
95
95
char c ;
96
96
int template_fmt_len = strlen (template_fmt );
@@ -106,23 +106,28 @@ static int template_fmt_size(char *template_fmt)
106
106
return j + 1 ;
107
107
}
108
108
109
- static int template_desc_init_fields (char * template_fmt ,
109
+ static int template_desc_init_fields (const char * template_fmt ,
110
110
struct ima_template_field * * * fields ,
111
111
int * num_fields )
112
112
{
113
- char * c , * template_fmt_ptr = template_fmt ;
113
+ char * c , * template_fmt_copy ;
114
114
int template_num_fields = template_fmt_size (template_fmt );
115
115
int i , result = 0 ;
116
116
117
117
if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX )
118
118
return - EINVAL ;
119
119
120
+ /* copying is needed as strsep() modifies the original buffer */
121
+ template_fmt_copy = kstrdup (template_fmt , GFP_KERNEL );
122
+ if (template_fmt_copy == NULL )
123
+ return - ENOMEM ;
124
+
120
125
* fields = kzalloc (template_num_fields * sizeof (* fields ), GFP_KERNEL );
121
126
if (* fields == NULL ) {
122
127
result = - ENOMEM ;
123
128
goto out ;
124
129
}
125
- for (i = 0 ; (c = strsep (& template_fmt_ptr , "|" )) != NULL &&
130
+ for (i = 0 ; (c = strsep (& template_fmt_copy , "|" )) != NULL &&
126
131
i < template_num_fields ; i ++ ) {
127
132
struct ima_template_field * f = lookup_template_field (c );
128
133
@@ -133,10 +138,12 @@ static int template_desc_init_fields(char *template_fmt,
133
138
(* fields )[i ] = f ;
134
139
}
135
140
* num_fields = i ;
136
- return 0 ;
137
141
out :
138
- kfree (* fields );
139
- * fields = NULL ;
142
+ if (result < 0 ) {
143
+ kfree (* fields );
144
+ * fields = NULL ;
145
+ }
146
+ kfree (template_fmt_copy );
140
147
return result ;
141
148
}
142
149
0 commit comments