@@ -81,7 +81,7 @@ sepgsql_get_label(Oid classId, Oid objectId, int32 subId)
8181if (security_get_initial_context_raw ("unlabeled" ,& unlabeled )< 0 )
8282ereport (ERROR ,
8383(errcode (ERRCODE_INTERNAL_ERROR ),
84- errmsg ("SELinux: failed to get initial security label" )));
84+ errmsg ("SELinux: failed to get initial security label: %m " )));
8585PG_TRY ();
8686{
8787label = pstrdup (unlabeled );
@@ -184,7 +184,7 @@ sepgsql_mcstrans_in(PG_FUNCTION_ARGS)
184184& raw_label )< 0 )
185185ereport (ERROR ,
186186(errcode (ERRCODE_INTERNAL_ERROR ),
187- errmsg ("SELinux: could not translate security label" )));
187+ errmsg ("SELinux: could not translate security label: %m " )));
188188
189189PG_TRY ();
190190{
@@ -224,7 +224,7 @@ sepgsql_mcstrans_out(PG_FUNCTION_ARGS)
224224& qual_label )< 0 )
225225ereport (ERROR ,
226226(errcode (ERRCODE_INTERNAL_ERROR ),
227- errmsg ("SELinux: could not translate security label" )));
227+ errmsg ("SELinux: could not translate security label: %m " )));
228228
229229PG_TRY ();
230230{
@@ -241,6 +241,51 @@ sepgsql_mcstrans_out(PG_FUNCTION_ARGS)
241241PG_RETURN_TEXT_P (cstring_to_text (result ));
242242}
243243
244+ /*
245+ * quote_object_names
246+ *
247+ * It tries to quote the supplied identifiers
248+ */
249+ static char *
250+ quote_object_name (const char * src1 ,const char * src2 ,
251+ const char * src3 ,const char * src4 )
252+ {
253+ StringInfoData result ;
254+ const char * temp ;
255+
256+ initStringInfo (& result );
257+
258+ if (src1 )
259+ {
260+ temp = quote_identifier (src1 );
261+ appendStringInfo (& result ,"%s" ,temp );
262+ if (src1 != temp )
263+ pfree ((void * )temp );
264+ }
265+ if (src2 )
266+ {
267+ temp = quote_identifier (src2 );
268+ appendStringInfo (& result ,".%s" ,temp );
269+ if (src2 != temp )
270+ pfree ((void * )temp );
271+ }
272+ if (src3 )
273+ {
274+ temp = quote_identifier (src3 );
275+ appendStringInfo (& result ,".%s" ,temp );
276+ if (src3 != temp )
277+ pfree ((void * )temp );
278+ }
279+ if (src4 )
280+ {
281+ temp = quote_identifier (src4 );
282+ appendStringInfo (& result ,".%s" ,temp );
283+ if (src4 != temp )
284+ pfree ((void * )temp );
285+ }
286+ return result .data ;
287+ }
288+
244289/*
245290 * exec_object_restorecon
246291 *
@@ -273,7 +318,7 @@ exec_object_restorecon(struct selabel_handle *sehnd, Oid catalogId)
273318Form_pg_class relForm ;
274319Form_pg_attribute attForm ;
275320Form_pg_proc proForm ;
276- char objname [ NAMEDATALEN * 4 + 10 ] ;
321+ char * objname ;
277322int objtype = 1234 ;
278323ObjectAddress object ;
279324security_context_t context ;
@@ -288,8 +333,10 @@ exec_object_restorecon(struct selabel_handle *sehnd, Oid catalogId)
288333nspForm = (Form_pg_namespace )GETSTRUCT (tuple );
289334
290335objtype = SELABEL_DB_SCHEMA ;
291- snprintf (objname ,sizeof (objname ),"%s.%s" ,
292- database_name ,NameStr (nspForm -> nspname ));
336+
337+ objname = quote_object_name (database_name ,
338+ NameStr (nspForm -> nspname ),
339+ NULL ,NULL );
293340
294341object .classId = NamespaceRelationId ;
295342object .objectId = HeapTupleGetOid (tuple );
@@ -309,9 +356,10 @@ exec_object_restorecon(struct selabel_handle *sehnd, Oid catalogId)
309356continue ;/* no need to assign security label */
310357
311358namespace_name = get_namespace_name (relForm -> relnamespace );
312- snprintf (objname ,sizeof (objname ),"%s.%s.%s" ,
313- database_name ,namespace_name ,
314- NameStr (relForm -> relname ));
359+ objname = quote_object_name (database_name ,
360+ namespace_name ,
361+ NameStr (relForm -> relname ),
362+ NULL );
315363pfree (namespace_name );
316364
317365object .classId = RelationRelationId ;
@@ -330,11 +378,12 @@ exec_object_restorecon(struct selabel_handle *sehnd, Oid catalogId)
330378namespace_id = get_rel_namespace (attForm -> attrelid );
331379namespace_name = get_namespace_name (namespace_id );
332380relation_name = get_rel_name (attForm -> attrelid );
333- snprintf ( objname , sizeof ( objname ), "%s.%s.%s.%s" ,
334- database_name , namespace_name ,
335- relation_name ,NameStr ( attForm -> attname ));
336- pfree ( relation_name );
381+ objname = quote_object_name ( database_name ,
382+ namespace_name ,
383+ relation_name ,
384+ NameStr ( attForm -> attname ) );
337385pfree (namespace_name );
386+ pfree (relation_name );
338387
339388object .classId = RelationRelationId ;
340389object .objectId = attForm -> attrelid ;
@@ -347,9 +396,10 @@ exec_object_restorecon(struct selabel_handle *sehnd, Oid catalogId)
347396objtype = SELABEL_DB_PROCEDURE ;
348397
349398namespace_name = get_namespace_name (proForm -> pronamespace );
350- snprintf (objname ,sizeof (objname ),"%s.%s.%s" ,
351- database_name ,namespace_name ,
352- NameStr (proForm -> proname ));
399+ objname = quote_object_name (database_name ,
400+ namespace_name ,
401+ NameStr (proForm -> proname ),
402+ NULL );
353403pfree (namespace_name );
354404
355405object .classId = ProcedureRelationId ;
@@ -359,6 +409,7 @@ exec_object_restorecon(struct selabel_handle *sehnd, Oid catalogId)
359409
360410default :
361411elog (ERROR ,"unexpected catalog id: %u" ,catalogId );
412+ objname = NULL ;/* for compiler quiet */
362413break ;
363414}
364415
@@ -389,7 +440,9 @@ exec_object_restorecon(struct selabel_handle *sehnd, Oid catalogId)
389440else
390441ereport (ERROR ,
391442(errcode (ERRCODE_INTERNAL_ERROR ),
392- errmsg ("SELinux: could not determine initial security label for %s (type=%d)" ,objname ,objtype )));
443+ errmsg ("SELinux: could not determine initial security label for %s (type=%d): %m" ,objname ,objtype )));
444+
445+ pfree (objname );
393446}
394447systable_endscan (sscan );
395448
@@ -449,7 +502,7 @@ sepgsql_restorecon(PG_FUNCTION_ARGS)
449502if (!sehnd )
450503ereport (ERROR ,
451504(errcode (ERRCODE_INTERNAL_ERROR ),
452- errmsg ("SELinux: failed to initialize labeling handle" )));
505+ errmsg ("SELinux: failed to initialize labeling handle: %m " )));
453506PG_TRY ();
454507{
455508/*