Skip to contentSkip to conversation

checkpolicy: Allow lineno > 1 for source file line

PATCH In review Inseob Kim 2 participants 3 messages Apr 30, 2025 – May 6, 2025 SELinux security module

lore
Share & tools
lore

Inseob Kim

author
#0 lore raw

PATCH v2 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.

Changes since v1:
- Leave set_source_file as-is to prevent regressions

---
 checkpolicy/policy_scan.l | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
checkpolicy/policy_scan.l +20 -1
index 5fb9ff37..7bdea427 100644
@@ -57,6 +57,7 @@ void yyfatal(const char *msg)
 #endif
 
 void set_source_file(const char *name);
+static void set_source_line_and_file(const char *line);
 
 char source_file[PATH_MAX];
 unsigned long source_lineno = 1;
@@ -297,7 +298,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_line_and_file(yytext+6); }
 #line[ ]{digit}+	        {
 				  errno = 0;
 				  source_lineno = strtoul(yytext+6, NULL, 10) - 1;
@@ -395,3 +396,21 @@ void set_source_file(const char *name)
 	if (strlen(source_file) && source_file[strlen(source_file)-1] == '"')
 		source_file[strlen(source_file)-1] = '\0';
 }
+
+void set_source_line_and_file(const char *line)
+{
+	char *name;
+	unsigned long lineno;
+	errno = 0;
+	lineno = strtoul(line, &name, 10) - 1;
+	if (errno) {
+		yywarn("source line number too big");
+	}
+	set_source_file(name + 2 /* skip a space and a quote */ );
+
+	/*
+	 * set_source_file sets source_lineno to 1.
+	 * Assign source_lineno after calling set_source_file.
+	 */
+	source_lineno = lineno;
+}
Signature · 1 line
2.49.0.901.g37484f566f-goog
Acked-by on v2 1/1
#1 lore raw
Quoted 7 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 53 lines

Changes since v1:
- Leave set_source_file as-is to prevent regressions

---
 checkpolicy/policy_scan.l | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
checkpolicy/policy_scan.l +20 -2
index 5fb9ff37..7bdea427 100644
@@ -57,6 +57,7 @@ void yyfatal(const char *msg)
 #endif
 
 void set_source_file(const char *name);
+static void set_source_line_and_file(const char *line);
 
 char source_file[PATH_MAX];
 unsigned long source_lineno = 1;
@@ -297,7 +298,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_line_and_file(yytext+6); }
 #line[ ]{digit}+               {
                                  errno = 0;
                                  source_lineno = strtoul(yytext+6, NULL, 10) - 1;
@@ -395,3 +396,21 @@ void set_source_file(const char *name)
        if (strlen(source_file) && source_file[strlen(source_file)-1] == '"')
                source_file[strlen(source_file)-1] = '\0';
 }
+
+void set_source_line_and_file(const char *line)
+{
+       char *name;
+       unsigned long lineno;
+       errno = 0;
+       lineno = strtoul(line, &name, 10) - 1;
+       if (errno) {
+               yywarn("source line number too big");
+       }
+       set_source_file(name + 2 /* skip a space and a quote */ );
+
+       /*
+        * set_source_file sets source_lineno to 1.
+        * Assign source_lineno after calling set_source_file.
+        */
+       source_lineno = lineno;
+}
--

2.49.0.901.g37484f566f-goog

#2 lore raw
Quoted 12 lines · James Carter
Quoted 7 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.

Merged.
Thanks,
Jim

Quoted 53 lines
Quoted 53 lines

Changes since v1:
- Leave set_source_file as-is to prevent regressions

---
 checkpolicy/policy_scan.l | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
checkpolicy/policy_scan.l +20 -2
index 5fb9ff37..7bdea427 100644
@@ -57,6 +57,7 @@ void yyfatal(const char *msg)
 #endif
 
 void set_source_file(const char *name);
+static void set_source_line_and_file(const char *line);
 
 char source_file[PATH_MAX];
 unsigned long source_lineno = 1;
@@ -297,7 +298,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_line_and_file(yytext+6); }
 #line[ ]{digit}+               {
                                  errno = 0;
                                  source_lineno = strtoul(yytext+6, NULL, 10) - 1;
@@ -395,3 +396,21 @@ void set_source_file(const char *name)
        if (strlen(source_file) && source_file[strlen(source_file)-1] == '"')
                source_file[strlen(source_file)-1] = '\0';
 }
+
+void set_source_line_and_file(const char *line)
+{
+       char *name;
+       unsigned long lineno;
+       errno = 0;
+       lineno = strtoul(line, &name, 10) - 1;
+       if (errno) {
+               yywarn("source line number too big");
+       }
+       set_source_file(name + 2 /* skip a space and a quote */ );
+
+       /*
+        * set_source_file sets source_lineno to 1.
+        * Assign source_lineno after calling set_source_file.
+        */
+       source_lineno = lineno;
+}
--

2.49.0.901.g37484f566f-goog