Skip to contentSkip to conversation

checkpolicy: Allow lineno > 1 for source file line

PATCH Superseded Inseob Kim 2 participants 3 messages Apr 4, 2025 – Apr 25, 2025 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Inseob Kim

author
#0 lore raw

PATCH 1/1 checkpolicy: Allow lineno > 1 for source file line

There are cases that the line number starts with a number greater than one, if preprocessor skipped the very first line. This extends #line syntax so any lineno is valid for lines with a source file path.

---
 checkpolicy/policy_scan.l | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
checkpolicy/policy_scan.l +11 -5
index 5fb9ff37..c418a629 100644
@@ -56,7 +56,7 @@ void yyfatal(const char *msg)
 #define YY_FATAL_ERROR(msg) yyfatal(msg)
 #endif
 
-void set_source_file(const char *name);
+void set_source_file(const char *line);
 
 char source_file[PATH_MAX];
 unsigned long source_lineno = 1;
@@ -297,7 +297,7 @@ GLBLUB				{ return(GLBLUB); }
 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*  { return(IPV6_ADDR); }
 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*"/"{digit}{1,3}	{ return(IPV6_CIDR); }
 {digit}+(\.({alnum}|[_.])*)?    { return(VERSION_IDENTIFIER); }
-#line[ ]1[ ]\"[^\n]*\"		{ set_source_file(yytext+9); }
+#line[ ]{digit}+[ ]\"[^\n]*\"	{ set_source_file(yytext+6); }
 #line[ ]{digit}+	        {
 				  errno = 0;
 				  source_lineno = strtoul(yytext+6, NULL, 10) - 1;
@@ -387,10 +387,16 @@ int yywarn(const char *msg)
 	return 0;
 }
 
-void set_source_file(const char *name)
+void set_source_file(const char *line)
 {
-	source_lineno = 1;
-	strncpy(source_file, name, sizeof(source_file)-1); 
+	char *name;
+	errno = 0;
+	source_lineno = strtoul(line, &name, 10) - 1;
+	if (errno) {
+		yywarn("source line number too big");
+	}
+	name += 2; /* skip a space and a quote */
+	strncpy(source_file, name, sizeof(source_file)-1);
 	source_file[sizeof(source_file)-1] = '\0';
 	if (strlen(source_file) && source_file[strlen(source_file)-1] == '"')
 		source_file[strlen(source_file)-1] = '\0';
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Inseob Kim

There are cases that the line number starts with a number greater than one, if preprocessor skipped the very first line. This extends #line syntax so any lineno is valid for lines with a source file path.

Quoted 50 lines
---
 checkpolicy/policy_scan.l | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
checkpolicy/policy_scan.l +11 -6
index 5fb9ff37..c418a629 100644
@@ -56,7 +56,7 @@ void yyfatal(const char *msg)
 #define YY_FATAL_ERROR(msg) yyfatal(msg)
 #endif
 
-void set_source_file(const char *name);
+void set_source_file(const char *line);
 
 char source_file[PATH_MAX];
 unsigned long source_lineno = 1;
@@ -297,7 +297,7 @@ GLBLUB                              { return(GLBLUB); }
 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*  { return(IPV6_ADDR); }
 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*"/"{digit}{1,3}        { return(IPV6_CIDR); }
 {digit}+(\.({alnum}|[_.])*)?    { return(VERSION_IDENTIFIER); }
-#line[ ]1[ ]\"[^\n]*\"         { set_source_file(yytext+9); }
+#line[ ]{digit}+[ ]\"[^\n]*\"  { set_source_file(yytext+6); }
 #line[ ]{digit}+               {
                                  errno = 0;
                                  source_lineno = strtoul(yytext+6, NULL, 10) - 1;
@@ -387,10 +387,16 @@ int yywarn(const char *msg)
        return 0;
 }
 
-void set_source_file(const char *name)
+void set_source_file(const char *line)
 {
-       source_lineno = 1;
-       strncpy(source_file, name, sizeof(source_file)-1);
+       char *name;
+       errno = 0;
+       source_lineno = strtoul(line, &name, 10) - 1;
+       if (errno) {
+               yywarn("source line number too big");
+       }
+       name += 2; /* skip a space and a quote */
+       strncpy(source_file, name, sizeof(source_file)-1);
        source_file[sizeof(source_file)-1] = '\0';
        if (strlen(source_file) && source_file[strlen(source_file)-1] == '"')
                source_file[strlen(source_file)-1] = '\0';
--

2.49.0.504.g3bcea36a83-goog

#2 lore raw
Quoted 11 lines · James Carter
Quoted 6 lines · Inseob Kim

There are cases that the line number starts with a number greater than one, if preprocessor skipped the very first line. This extends #line syntax so any lineno is valid for lines with a source file path.

Removing my ACK for now because I found a problem. See below.

Quoted 30 lines
Quoted 30 lines
---
 checkpolicy/policy_scan.l | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
checkpolicy/policy_scan.l +2 -2
index 5fb9ff37..c418a629 100644
@@ -56,7 +56,7 @@ void yyfatal(const char *msg)
 #define YY_FATAL_ERROR(msg) yyfatal(msg)
 #endif
 
-void set_source_file(const char *name);
+void set_source_file(const char *line);
 
 char source_file[PATH_MAX];
 unsigned long source_lineno = 1;
@@ -297,7 +297,7 @@ GLBLUB                              { return(GLBLUB); }
 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*  { return(IPV6_ADDR); }
 {hexval}{0,4}":"{hexval}{0,4}":"({hexval}|[:.])*"/"{digit}{1,3}        { return(IPV6_CIDR); }
 {digit}+(\.({alnum}|[_.])*)?    { return(VERSION_IDENTIFIER); }
-#line[ ]1[ ]\"[^\n]*\"         { set_source_file(yytext+9); }
+#line[ ]{digit}+[ ]\"[^\n]*\"  { set_source_file(yytext+6); }
 #line[ ]{digit}+               {
                                  errno = 0;
                                  source_lineno = strtoul(yytext+6, NULL, 10) - 1;
@@ -387,10 +387,16 @@ int yywarn(const char *msg)
        return 0;
 }
 
Quoted 2 lines

checkpolicy/policy_scan.l int yywarn(const char *msg) · line 390

Quoted 2 lines
checkpolicy/policy_scan.l +1 -1
-void set_source_file(const char *name)
+void set_source_file(const char *line)

set_source_file() is called in policy_define.c:init_parser() where it passes in the file name.

I tested it by specifically setting line numbers which works, but if you don't do that there are problems.

Maybe the best way would be to break out the part that sets the source_file to a function called set_source_file() and then have another function called set_source_line_and_file() which sets the lineno like below and then calls set_source_file().

Thanks,
Jim

Quoted 18 lines
Quoted 18 lines
checkpolicy/policy_scan.l +8 -3
 {
-       source_lineno = 1;
-       strncpy(source_file, name, sizeof(source_file)-1);
+       char *name;
+       errno = 0;
+       source_lineno = strtoul(line, &name, 10) - 1;
+       if (errno) {
+               yywarn("source line number too big");
+       }
+       name += 2; /* skip a space and a quote */
+       strncpy(source_file, name, sizeof(source_file)-1);
        source_file[sizeof(source_file)-1] = '\0';
        if (strlen(source_file) && source_file[strlen(source_file)-1] == '"')
                source_file[strlen(source_file)-1] = '\0';
--

2.49.0.504.g3bcea36a83-goog